Reputation: 18966
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>
?
Upvotes: 531
Views: 1991511
Reputation: 10794
If you check the API for List
you'll notice it says:
Interface List<E>
Being an interface
means it cannot be instantiated (no new List()
is possible).
If you check that link, you'll find some class
es that implement List
:
All Known Implementing Classes:
AbstractList
,AbstractSequentialList
,ArrayList
,AttributeList
,CopyOnWriteArrayList
,LinkedList
,RoleList
,RoleUnresolvedList
,Stack
,Vector
Some of those can be instantiated (the ones that are not defined as abstract class
). Use their links to know more about them, I.E: to know which fits better your needs.
The 3 most commonly used ones probably are:
List<String> supplierNames1 = new ArrayList<String>();
List<String> supplierNames2 = new LinkedList<String>();
List<String> supplierNames3 = new Vector<String>();
Bonus:
You can also instantiate it with values, in an easier way, using the Arrays
class
, as follows:
List<String> supplierNames = Arrays.asList("sup1", "sup2", "sup3");
System.out.println(supplierNames.get(1));
But note you are not allowed to add more elements to that list, as it's fixed-size
.
Upvotes: 892
Reputation: 61
Instead of :
List<String> supplierNames = new List<String>();
Write this if you are using latest JDK:
List<String> supplierNames = new ArrayList<>();
It's the correct way of initializing a List.
Upvotes: 3
Reputation: 23323
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
Upvotes: 0
Reputation: 7211
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");
Upvotes: 206
Reputation: 605
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.
Upvotes: 3
Reputation: 7459
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
Upvotes: 7
Reputation: 861
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>();
Upvotes: 43
Reputation: 5852
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
Upvotes: 11
Reputation: 8027
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>();
Upvotes: 18
Reputation: 8874
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>();
Upvotes: 9
Reputation: 6572
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.
Upvotes: 5
Reputation: 604
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.
Upvotes: 7
Reputation: 3183
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>();
Upvotes: 15