33

In C# I can create a collection of some kind and initialize it with data on the same line.

var foo = new List<string> {"one","two","three"};

Is there an equivalent way to do this in Java?

6 Answers6

49

If you need a read-only List

List<String> numbers = Arrays.asList("one","two","three");

// Can't add since the list is immutable
numbers.add("four"); // java.lang.UnsupportedOperationException

If you would like to modify the List later on.

List<String> numbers2 = new ArrayList<String>(
                            Arrays.asList("one","two","three"));
numbers2.add("four");

System.out.println(numbers2); // [one, two, three, four]
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • 3
    It's worth noting that `Arrays.asList` produces a **MUTABLE** list. The documentations indicates that the resulting list has a fixed size but `numbers.set(0, "one")` will absolutely modify both the list and underlying array. – Chris Kerekes Apr 05 '18 at 15:55
  • For interface driven people.........this will work too: Collection inlineCollection = Arrays.asList( myObjectInstantiatedOne, myObjectInstantiatedTwo, myObjectInstantiatedThree ); Thanks for this answer Ravi! – granadaCoder Aug 31 '18 at 12:16
10

You can use Arrays.asList(T... a)

List<String> foo = Arrays.asList("one","two","three");

As Boris mentions in the comments the resulting List is immutable (ie. read-only). You will need to convert it to an ArrayList or similar in order to modify the collection:

List<String> foo = new ArrayList<String>(Arrays.asList("one","two","three"));

You can also create the List using an anonymous subclass and initializer:

List<String> foo = new ArrayList<String>() {
    {
        add("one");
        add("two");
        add("three");
    }
};
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
4

I prefer doing this using the Guava (formerly called Google Collections) library, which both removes the need to write the type down again AND has all kinds of ways of adding data straight away.

Example: List<YourClass> yourList = Lists.newArrayList();

Or with adding data: List<YourClass> yourList = Lists.newArrayList(yourClass1, yourclass2);

The same works for all other kinds of collections and their various implementations. Another example: Set<String> treeSet = Sets.newTreeSet();

You can find it at https://code.google.com/p/guava-libraries/

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73
1

The best that I've been able to come up with is:

final List<String> foo = new ArrayList<String>() {{
  add("one");
  add("two");
  add("three");
}};

Basically, that says that you are creating an anonymous sub-class of the ArrayList class which is then statically initialized using "one", "two", "three".

Ryan Ransford
  • 3,224
  • 28
  • 35
0
List<String> list = Arrays.asList("one","two","three")
Nailgun
  • 3,999
  • 4
  • 31
  • 46
0
List<String> numbers = Arrays.asList("one","two","three");

As Boris commented, it makes your numbers immutable.

Yes,You can, but with two lines.

List<String> numbers= new ArrayList<String>();
Collections.addAll(numbers,"one","two","three");

If you still want in Only in one line ,With Gauva

List<String> numbers= Lists.newArrayList("one","two","three");
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307