Initialize List of String in java

In this post, we will see how to initialize List of String in java.

Can you initialize List of String as below:

You can't because List is an interface and it can not be instantiated with new List().

You need to instantiate it with the class that implements the List interface.

Here are the common java Collections classes which implement List interface.

In most of the cases, you will initialize List with ArrayList as below.

If you are using java 7 or greater than you can use diamond operator with generics.

Initialize List of Strings with values

There are many ways to initialize list of Strings with values.

Arrays’s asList

You can use Arrays’s asList method to initialize list with values.

Stream.of (Java 8)

You can use java 8‘s Stream to initialize list of String with values.

List.of (Java 9)

Finally, java has introduced a of() method in List class to initialize list with values in java 9.

Using ArrayList’s add method

You can obviously initialize ArrayList with new operator and then use add method to add element to the list.

Using guava library

You can use guava library as well.

Here is the complete example.

Output:

[India, China, Bhutan] [India, China, Bhutan] [India, China, Bhutan] [India, China, Bhutan]

That’s all about how to initialize List of String in java.

Related Posts

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusion In this […]

  • Java String contains IgnoreCase
    23 June

    Java String contains IgnoreCase

    Table of ContentsUsing String.toLowerCase()Using Pattern.compile()Using String’s MatchesUsing StringUtils.containsIgnoreCase()Complete program for Java String contains IgnoreCaseConclusion We often have to check if a string contains a substring or not in a case-insensitive manner. There are multiple ways to do so. In this article, we will take a look at these methods for java String contains IgnoreCase checks. […]

  • Find and count occurences of substring in String in Java
    02 June

    Find and count occurrences of substring in string in java

    Table of ContentsUsing the indexOf() methodUsing regular expressionUsing split() methodUsing the countMatches() method from Apache Commons library In this post, we will see how to find and count occurrences of substring in string in java. Using the indexOf() method The indexOf() method in java is a specialized function to find the index of the first […]

  • Print double quotes in java
    23 May

    Print double quotes in java

    Table of ContentsPrint double quotes in java using backslash characterPrint double quotes in java using unicode characterPrint double quotes in java by appending double quote character 💡 Outline You can print double quotes in java by escape double quotes using backslash character(\). [crayon-60ef0ac0ed785782052568/] When you print on console using System.out.println, you use double quotes and […]

  • Java Split String by Space
    13 May

    Java split string by space

    Learn about how to split String in java in different ways.

  • Split String by new line in java
    09 May

    How to split String by newline in java

    Learn about how to split String by newline in java

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.