package org.kodejava.util;
import java.util.ArrayList;
import java.util.LinkedList;
public class CollectionToArrayList {
public static void main(String[] args) {
// We create LinkedList collection type at put some values
// in it. Here we put A, B, C and D letter into it.
LinkedList<String> linkedList = new LinkedList<>();
linkedList.push("A");
linkedList.push("B");
linkedList.push("C");
linkedList.push("D");
// Let say you want to convert it to other type of collection,
// for instance here we convert it into ArrayList. To do it
// we can pass the collection created above as a parameter to
// ArrayList constructor.
ArrayList<String> arrayList = new ArrayList<>(linkedList);
// Now we have converted the collection into ArrayList and
// printed what is inside.
for (String s : arrayList) {
System.out.println("s = " + s);
}
}
}
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023