What is the shortest way to initialize List of strings in java?

Vladimir :

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.

Jon Skeet :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using Java 8, what is the most concise way of creating a sorted AND grouped list of Strings

What is the best way of using Arrays.asList() to initialize a List

What's the quickest way to compare strings in Java?

What is the shortest way to delegate unimplemented methods to a contained object in Java?

What is the best way to initialize a bean?

Which Way of List Initialize Is Better

Initialize structs from list of strings

What is a better way to initialize varibles in Java with less code?

What is the shortest way to create a record form list?

What is the shortest way to instantiate a list in Haskell?

Shortest way to check wether a list of strings occur in a string

Javascript what is the best way to filter objects based on a list of strings in an array?

What is the right way to initialize a QList?

What is the better way to add pairs of strings into List<T>?

Write a function named shortest() that finds the length of the shortest string in a list of strings

Shortest way to extract Map from List in Java

What is the easiest way to initialize a 2d array from a vector of strings?

Initialize a static const list of strings

What's the shortest way of converting list of lists to a string, one inner list per line?

What is the cleanest way to initialize dynamic list in Python?

What is the best way to print position of items in the list of strings using python

What is the Pythonic way to check if a list of strings is sorted or not?

What's the shortest way to find a dict in a list?

Shortest duplicate in a list of strings

Initialize a list of strings with a function

The shortest way to initialize a point array?

What is the best way to search for a value in a list of list of strings?

What is the proper way to check string or list of strings length in JS?

What is the shortest way to drop partial duplicates from a list of tuple in Python without using Pandas?