How can I initialize a LinkedList with entries/values in it?

J L picture J L · Apr 27, 2012 · Viewed 95.1k times · Source

So I know how to have a linked list and use add method to input entries by entries. However, I do not want to add entries by entries. Is there a way to declare a linkedlist with initial values in the list?

For example, if I want to have 1.0 & 2.0 in the list is there something I can do in one line? Something like:

List<Double> temp1 = new LinkedList<Double>(1,2);

Answer

Eugene Retunsky picture Eugene Retunsky · Apr 27, 2012

You can do that this way:

List<Double> temp1 = new LinkedList<Double>(Arrays.asList(1.0, 2.0));