[java] How to initialize List<String> object in Java?

I can not initialize a List as in the following code:

List<String> supplierNames = new List<String>();
supplierNames.add("sup1");
supplierNames.add("sup2");
supplierNames.add("sup3");
System.out.println(supplierNames.get(1));

I face the following error:

Cannot instantiate the type List<String>

How can I instantiate List<String>?

This question is related to java list

The answer is


Just in case, any one still lingering around this question. Because, i see one or two new users again asking the same question and everyone telling then , No you can't do that, Dear Prudence, Apart from all the answers given here, I would like to provide additional Information - Yes you can actually do, List list = new List(); But at the cost of writing implementations of all the methods of Interfaces. The notion is not simply List list = new List(); but

List<Integer> list = new List<Integer>(){

        @Override
        public int size() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean contains(Object o) {
            // TODO Auto-generated method stub
            return false;
        }

..... and So on (Cant write all methods.)

This is an example of Anonymous class. Its correct when someone states , No you cant instantiate an interface, and that's right. But you can never say , You CANT write List list = new List(); but, evidently you can do that and that's a hard statement to make that You can't do.


In most cases you want simple ArrayList - an implementation of List

Before JDK version 7

List<String> list = new ArrayList<String>();

JDK 7 and later you can use the diamond operator

List<String> list = new ArrayList<>();

Further informations are written here Oracle documentation - Collections


We created soyuz-to to simplify 1 problem: how to convert X to Y (e.g. String to Integer). Constructing of an object is also kind of conversion so it has a simple function to construct Map, List, Set:

import io.thedocs.soyuz.to;

List<String> names = to.list("John", "Fedor");

Please check it - it has a lot of other useful features


If you just want to create an immutable List<T> with only one object in it, you can use this API:

List<String> oneObjectList = Collections.singletonList("theOnlyObjectā€¯);

More info: docs


Depending on what kind of List you want to use, something like

List<String> supplierNames = new ArrayList<String>();

should get you going.

List is the interface, ArrayList is one implementation of the List interface. More implementations that may better suit your needs can be found by reading the JavaDocs of the List interface.


You will need to use ArrayList<String> or such.

List<String> is an interface.

Use this:

import java.util.ArrayList;

...

List<String> supplierNames = new ArrayList<String>();

List is an interface, and you can not initialize an interface. Instantiate an implementing class instead.

Like:

List<String> abc = new ArrayList<String>();
List<String> xyz = new LinkedList<String>();

List is an Interface, you cannot instantiate an Interface, because interface is a convention, what methods should have your classes. In order to instantiate, you need some realizations(implementations) of that interface. Try the below code with very popular implementations of List interface:

List<String> supplierNames = new ArrayList<String>(); 

or

List<String> supplierNames = new LinkedList<String>();

List is an Interface . You cant use List to initialize it.

  List<String> supplierNames = new ArrayList<String>();

These are the some of List impelemented classes,

ArrayList, LinkedList, Vector

You could use any of this as per your requirement. These each classes have its own features.


Can't instantiate an interface but there are few implementations:

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

// creates immutable lists, so you can't modify such list 
List<String> immutableList = List.of("one", "two", "three");

// if we want mutable list we can copy content of immutable list 
// to mutable one for instance via copy-constructor (which creates shallow copy)
List<String> mutableList = new ArrayList<>(List.of("one", "two", "three"));

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

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

List is just an interface, a definition of some generic list. You need to provide an implementation of this list interface. Two most common are:

ArrayList - a list implemented over an array

List<String> supplierNames = new ArrayList<String>();

LinkedList - a list implemented like an interconnected chain of elements

List<String> supplierNames = new LinkedList<String>();