69

I am searching for the shortest way (in code) to initialize list of strings and array of strings, i.e. list/array containing "s1", "s2", "s3" string elements.

radoh
  • 4,554
  • 5
  • 30
  • 45
Vladimir
  • 12,753
  • 19
  • 62
  • 77

7 Answers7

119

There are various options. Personally I like using Guava:

List<String> strings = Lists.newArrayList("s1", "s2", "s3");

(Guava's a library worth having anyway, of course :)

Using just the JDK, you could use:

List<String> strings = Arrays.asList("s1", "s2", "s3");

Note that this will return an ArrayList, but that's not the normal java.util.ArrayList - it's an internal one which is mutable but fixed-size.

Personally I prefer the Guava version as it makes it clear what's going on (the list implementation which will be returned). It's also still clear what's going on if you statically import the method:

// import static com.google.common.collect.Lists.newArrayList;
List<String> strings = newArrayList("s1", "s2", "s3");

... whereas if you statically import asList it looks a little odder.

Another Guava option, if you don't want a modifiable-in-any-way list:

ImmutableList<String> strings = ImmutableList.of("s1", "s2", "s3");

I typically want to either have a completely mutable list (in which case Lists.newArrayList is best) or a completely immutable list (in which case ImmutableList.of is best). It's rare that I really want a mutable-but-fixed-size list.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • why not just Arrays.asList("s1", "s2", "s3")? – BegemoT Jun 29 '11 at 12:11
  • 1
    @BegemoT: I was adding that - personally I prefer the explicit nature of the Guava version. – Jon Skeet Jun 29 '11 at 12:13
  • *which is unmodifiable* -- It's actually modifiable (but fixed-sized). – aioobe Jun 29 '11 at 12:35
  • @Matt: No problem. I'll delete the one replying to you, and then delete *this* one soon, if you want to delete your second one too :) – Jon Skeet Jun 29 '11 at 16:21
  • With all respect to Jon Skeet. Isn't it better to use Arrays.asList() instead of ImmutableList.of(), as the later, will maintain the copied array. Can you please share some light on this? – Ashish Lohia Aug 31 '17 at 09:26
  • @AshishLohia: Not if you want the list to be immutable. That's why I give multiple options - the best approach depends on what you want the result to be. – Jon Skeet Aug 31 '17 at 10:54
  • I completely agree with you @JonSkeet . Thanks for clarifying. – Ashish Lohia Sep 01 '17 at 03:36
38

Java 9+

Java 9 introduces a convenience method List.of used as follows:

List<String> l = List.of("s1", "s2", "s3");

Java 8 and older

Here are a few alternatives:

// Short, but the resulting list is fixed size.
List<String> list1 = Arrays.asList("s1", "s2", "s3");

// Similar to above, but the resulting list can grow.
List<String> list2 = new ArrayList<>(Arrays.asList("s1", "s2", "s3"));

// Using initialization block. Useful if you need to "compute" the strings.
List<String> list3 = new ArrayList<String>() {{
    add("s1");
    add("s2");
    add("s3");
}};

When it comes to arrays, you could initialize it at the point of declaration like this:

String[] arr = { "s1", "s2", "s3" };

If you need to reinitialize it or create it without storing it in a variable, you do

new String[] { "s1", "s2", "s3" }

If the string constants are may though, it would look like

String[] arr = { "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10",
                 "s11", "s12", "s13" };

In these cases I usually prefer writing

String[] arr = "s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13".split(",");
aioobe
  • 413,195
  • 112
  • 811
  • 826
15
List<String> stringList = Arrays.asList("s1", "s2", "s3");

All these objects exists in the JDK.

PS: As aioobe stated, this makes the list fixed-sized.

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
4

JDK2

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

JDK7

//diamond operator
List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");

JDK8

List<String> list = Stream.of("one", "two", "three").collect(Collectors.toList());

JDK9

List<String> list = List.of("one", "two", "three");

Plus there are lots of other ways supplied by other libraries like Guava.

LazerBanana
  • 6,865
  • 3
  • 28
  • 47
3

You can use the Arrays class in the standard Java API: http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList(T...)

List<String> strings = Arrays.asList("s1", "s2", "s3");

Be aware that the resultning list is fixed-size (you cannot add to it).

Mathias Schwarz
  • 7,099
  • 23
  • 28
2

With Eclipse Collections, you can write the following:

List<String> list = Lists.mutable.with("s1", "s2", "s3");

You can also be more specific about the types and whether they are Mutable or Immutable.

MutableList<String> mList = Lists.mutable.with("s1", "s2", "s3");
ImmutableList<String> iList = Lists.immutable.with("s1", "s2", "s3");

You can also do the same with Sets, Bags and Maps:

Set<String> set = Sets.mutable.with("s1", "s2", "s3");
MutableSet<String> mSet = Sets.mutable.with("s1", "s2", "s3");
ImmutableSet<String> iSet = Sets.immutable.with("s1", "s2", "s3");

Bag<String> bag = Bags.mutable.with("s1", "s2", "s3");
MutableBag<String> mBag = Bags.mutable.with("s1", "s2", "s3");
ImmutableBag<String> iBag = Bags.immutable.with("s1", "s2", "s3");

Map<String, String> map = 
    Maps.mutable.with("s1", "s1", "s2", "s2", "s3", "s3");
MutableMap<String, String> mMap = 
    Maps.mutable.with("s1", "s1", "s2", "s2", "s3", "s3");
ImmutableMap<String, String> iMap = 
    Maps.immutable.with("s1", "s1", "s2", "s2", "s3", "s3");

There are factories for SortedSets, SortedBags and SortedMaps as well.

SortedSet<String> sortedSet = SortedSets.mutable.with("s1", "s2", "s3");
MutableSortedSet<String> mSortedSet = SortedSets.mutable.with("s1", "s2", "s3");
ImmutableSortedSet<String> iSortedSet = SortedSets.immutable.with("s1", "s2", "s3");

SortedBag<String> sortedBag = SortedBags.mutable.with("s1", "s2", "s3");
MutableSortedBag<String> mSortedBag = SortedBags.mutable.with("s1", "s2", "s3");
ImmutableSortedBag<String> iSortedBag = SortedBags.immutable.with("s1", "s2", "s3");

SortedMap<String, String> sortedMap =
        SortedMaps.mutable.with("s1", "s1", "s2", "s2", "s3","s3");
MutableSortedMap<String, String> mSortedMap =
        SortedMaps.mutable.with("s1", "s1", "s2", "s2", "s3","s3");
ImmutableSortedMap<String, String> iSortedMap =
        SortedMaps.immutable.with("s1", "s1", "s2", "s2", "s3","s3");

Note: I am a committer for Eclipse Collections.

Donald Raab
  • 6,458
  • 2
  • 36
  • 44
0

I am not familiar with Guava, but almost all solutions mentioned in other answers and using the JDK suffer have some flaws:

  1. The Arrays.asList method returns a fixed-size list.

  2. {{Double-brace initialization}} is known to create classpath clutter and slow down execution. It also requires one line of code per element.

  3. Java 9: the static factory method List.ofreturns a structurally immutable list. Moreover, List.of is value-based, and therefore its contract does not guarantee that it will return a new object each time.


Here is a Java 8 one-liner for gathering multiple individual objects into an ArrayList, or any collection for that matter.

List<String> list = Stream.of("s1", "s2", "s3").collect(Collectors.toCollection(ArrayList::new));

Note: aioobe's solution of copying a transitory collection (new ArrayList<>(Arrays.asList("s1", "s2", "s3"))) is also excellent.

Community
  • 1
  • 1
MikaelF
  • 3,518
  • 4
  • 20
  • 33