How to make a new List in Java

user93796 picture user93796 · May 13, 2009 · Viewed 2.1M times · Source

We create a Set as:

Set myset = new HashSet()

How do we create a List in Java?

Answer

Dan Vinton picture Dan Vinton · May 13, 2009
List myList = new ArrayList();

or with generics (Java 7 or later)

List<MyType> myList = new ArrayList<>();

or with generics (Old java versions)

List<MyType> myList = new ArrayList<MyType>();