6 ways to Initialize List of String in Java

In this post, I will be sharing how to initialize List of String in Java. There are six ways to achieve our goal:
1. Using ArrayList
2. Using Java 8 Stream.of()
3. Using Java 9 List.of()
4. Using Array's asList() method
5. Using ArrayList add() method
6. Using Guava library

Read Also: How to Iterate list of lists in Java

1. Initialize List of String Without Values

1. Using ArrayList


The easiest way to initialize the List of String without values is using ArrayList.
 List<String> list1 = new ArrayList<>(); 

Similarly, we can initialize the List of String without values using LinkedList as shown below:
List<String> list2 = new LinkedList<>(); 

2. Initialize List of String With Values

2. Using Java 8 Stream.of()


You can use the Java 8 Stream.of() method to initialize the list of string with values.
 import java.util.*;
import java.util.stream.*;
public class InitializeListOfString {
    public static void main(String args[]) {
      // Using Java 8     
      List<String> fruits = Stream.of("Apple", "Banana", "Orange").collect(Collectors.toList());   
      System.out.println(fruits);
    }
}


Output:
[Apple, Banana, Orange]

3. Using Java 9 List.of()


List.of() method is introduced in Java 9. It can be used to initialize the list of string with values as shown below:
 import java.util.List;
public class InitializeListOfString2 {
    public static void main(String args[]) {
      // Using Java 9     
      List<String> fruits = List.of("Apple", "Blackberry", "Strawberry");
      System.out.println(fruits);
    }
}


Output:
[Apple, Blackberry, Strawberry]

4. Using Arrays.asList() method


You can use Arrays class asList() method to initialize the list of string with values as shown below:
 import java.util.*;
public class InitializeListOfString3 {
    public static void main(String args[]) {
      // Using Arrays.asList() method     
      List<String> fruits = Arrays.asList("Apple", "Grape", "Pear");
      System.out.println(fruits);
    }
}


Output:
[Apple, Grape, Pear]

5. Using ArrayList class add() method


You can also use ArrayList class add() method to initialize list of string with values as shown below:
 import java.util.*;
public class InitializeListOfString4 {
    public static void main(String args[]) {
      // Using ArrayList class add() method  
      List<String> fruits = new ArrayList<>(); 
      fruits.add("Apple");
      fruits.add("Guava");
      fruits.add("Litchi");
      System.out.println(fruits);
    }
}


Output:
[Apple, Guava, Litchi]

6. Using Guava library


You can use the guava library too to achieve our goal of initializing a list of string with values.
 import static com.google.common.collect.Lists.newArrayList;
public class InitializeListOfString5 {
    public static void main(String args[]) {
      // Using Guava library newArrayList method  
      List<String> fruits = newArrayList("Apple", "Pomegranate", "Banana");
      System.out.println(fruits);
    }
}


Output:
[Apple, Pomegranate, Banana]

3. Complete Example

Below is the complete example:

 import java.util.*;
import java.util.stream.*;

public class InitializeListOfStringComplete {

    public static void main(String args[]) {

      // Using Java 9 List.of() method    
      List<String> fruits = List.of("Apple", "Blackberry", "Strawberry");
      System.out.println(fruits);

      // Using Java 8 Stream.of() method
      List<String> fruits2 = Stream.of("Apple", "Banana", "Orange").collect(Collectors.toList());   
      System.out.println(fruits2);

      // Using Arrays.asList() method
      List<String> fruits3 = Arrays.asList("Apple", "Grape", "Pear");
      System.out.println(fruits3);

      // Using ArrayList class add() method
      List<String> fruits4 = new ArrayList<>(); 
      fruits4.add("Apple");
      fruits4.add("Guava");
      fruits4.add("Litchi");
      System.out.println(fruits4);
    }
}


Output:
[Apple, Blackberry, Strawberry]
[Apple, Banana, Orange]
[Apple, Grape, Pear]
[Apple, Guava, Litchi]

That's all for today. Please mention in the comments in case you have any questions related to different ways to initialize list of string in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry