Example
In this example we are converting a LinkedList to ArrayList. We have a LinkedList of Strings in which we are storing names of 5 peoples. Later after conversion we are displaying the elements of ArrayList to ensure that ArrayList is having same elements that we have in LinkedList. The complete program is as follows:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ConvertExample {
public static void main(String[] args) {
LinkedList<String> linkedlist = new LinkedList<String>();
linkedlist.add("Harry");
linkedlist.add("Jack");
linkedlist.add("Tim");
linkedlist.add("Rick");
linkedlist.add("Rock");
List<String> list = new ArrayList<String>(linkedlist);
for (String str : list){
System.out.println(str);
}
}
}
Output:
Harry Jack Tim Rick Rock
Karthick says
what is difference between
List lstInteger = ArrayList();
ArrayList arrlstInteger = ArrayList();