8
  List<String> ll = new LinkedList<String>("String1","String2",...);

I want something like above. Is the above line possible in java...?

Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212
  • 1
    It would have taken you less time to compile and run the code then creating this question. – Woot4Moo Jun 09 '11 at 21:13
  • 3
    @Woot4Moo: He's not asking whether this syntax works. He's asking if there's a simple way like this to instantiate and initialize a LinkedList with a set of known values. – StriplingWarrior Jun 09 '11 at 21:15
  • 1
    @StriplingWarrior that is why the Javadocs exist you can very quickly look at the constructor summary. This question offers zero value. – Woot4Moo Jun 09 '11 at 21:17
  • @Woot4Moo as you can see, there is an answer to his question. – Marcelo Jun 09 '11 at 21:20
  • Seems a reasonable question to me - he's wanting an all in one way to instantiate a Java Collection. It's not going to be clear how to do this from the API docs as the answers using Arrays.asList prove. You can also use Collections.addAll with varargs but that's two lines. Voting to close this is premature IMO. +1. And the 4 upvotes seem to agree. – planetjones Jun 09 '11 at 21:23
  • @Marcelo This is a bad question, the answer is in the JavaDocs and is not providing any value as I stated previously. SO is not a content farm. Also, had the OP taken 20-30 seconds and searched the constructor summary for LinkedList in Java he would have found the following `LinkedList(Collection extends E> c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.` – Woot4Moo Jun 09 '11 at 21:23
  • @Woot4Moo: The javadocs don't tell you a concise way to create a collection to pass into that constructor. It's a very valid question. – StriplingWarrior Jun 09 '11 at 21:29
  • thanks everyone. I don't understand why some people think in stackoverflow that some questions are bad. I would rather suggest that comment only if you like to answer the question and don't clutter the whole area by writing all rubbish. I would like to thank – Saurabh Kumar Jun 09 '11 at 21:29
  • planetjones and Swarrior who really understood my question. – Saurabh Kumar Jun 09 '11 at 21:30
  • @Woot4Moo, besides the extremely useless question, if one doesn't know why should use a LinkedList they would be better off without. – bestsss Jun 09 '11 at 21:43
  • @Saurabh, the intent of the question is quite clear but it's asked in a quite bad way, noneless; actually why do you need a linkedList? – bestsss Jun 09 '11 at 21:44

7 Answers7

10

not directly but

List<String> ll = new LinkedList<String>(Arrays.asList("String1","String2",...));

is what you're looking for

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
2

Have a look at Guava. It provides various static utility functions (and much much more), such as

Lists.newArrayList("a", "b", "c")

and similar for other data structures

jvdneste
  • 1,677
  • 1
  • 12
  • 14
2

An alternative approach using double-braces is shown in this earlier StackOverflow page.

Community
  • 1
  • 1
Andrew Lazarus
  • 18,205
  • 3
  • 35
  • 53
1

No, but something similar:

LinkedList<String> linkedList = new LinkedList<String>(Arrays.asList("String1", "String2", "String3", "String4", ...));
helpermethod
  • 59,493
  • 71
  • 188
  • 276
0

I prefers option like this:

static final List<Integer> nums = new LinkedList<Integer>() {{
add(1);
add(2);
add(3); }};

And no matter what time of list.

VKostenc
  • 1,140
  • 14
  • 19
0

I usually use Arrays.asList("String1", "String2") for that purpose.

If you need to add elements to the list afterwards, use

List<String> ll = new ArrayList<String>(Arrays.asList("String1", "String2"));

This will give you a list that can be extended.

tonio
  • 10,355
  • 2
  • 46
  • 60
0

No not in the way you gave an example of.

You can pass a Collection however.

See http://download.oracle.com/javase/1.5.0/docs/api/java/util/LinkedList.html

Colin
  • 2,001
  • 13
  • 28