views:

6105

answers:

10

We create a Set as

Set myset = new HashSet()

How do we create a List in Java?

+1  A: 

One example:

List somelist = new ArrayList();

You can look at the javadoc for List and find all known implementing classes of the List interface that are included with the java api.

Alex B
+2  A: 
List list = new ArrayList();
Paulo Guedes
+21  A: 
List myList = new ArrayList();

or with generics

List<MyType> myList = new ArrayList<MyType>();
Dan Vinton
+11  A: 

First read this, then read this and this. 9 times out of 10 you'll use one of those two implementations.

In fact, just read Sun's Guide to the Collections framework.

Adam Jaskiewicz
I'd even add "8 times out of 10" you'll use ArrayList, just because it simply doesn't matter in 9.9 times out of 10.
Joachim Sauer
LinkedList is semantically appropriate when you only really care about the ends.
Adam Jaskiewicz
LinkedLists are excellent if you just are going to iterate over them.To mee, it seems as if linked lists are more elegant, but maybe it's just because i learned lisp before Java.
KarlP
@Karlp Agreed. I would say it's about 50/50 most of the time between ArrayList and LinkedList, and the answer isn't always about the complexity of the operations; more often it's simply what feels right for the problem at hand.
Adam Jaskiewicz
+7  A: 

Additionally, if you want to create a list that has things in it:

List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");
Aaron Maenpaa
The caveat is that this type of list (the one returned by asList()) is immutable.
Avrom
@Avron - wrong: it is only fixed size: you can not change the size, but you can change the content (caveat of the caveat?)
Carlos Heuberger
+1  A: 
//simple example creating a list form a string array

String[] myStrings = new String[] {"Elem1","Elem2","Elem3","Elem4","Elem5"};

List mylist = Arrays.asList(myStrings );

//getting an iterator object to browse list items

Iterator itr= mylist.iterator();

System.out.println("Displaying List Elements,");

while(itr.hasNext())

  System.out.println(itr.next());
Izabela
+1  A: 

Sometimes - but only very rarely - instead of a new ArrayList, you may want a new LinkedList. Start out with ArrayList and if you have performance problems and evidence that the list is the problem, and a lot of adding and deleting to that list - then - not before - switch to a LinkedList and see if things improve. But in the main, stick with ArrayList and all will be fine.

Carl Manaster
+6  A: 

List is just an interface just as Set.

Like HashSet is an implementation of a Set which has certain properties in regards to add / lookup / remove performance, ArrayList is the bare implementation of a List.

If you have a look at the documentation for the respective interfaces you will find "All Known Implementing Classes" and you can decide which one is more suitable for your needs.

Chances are that it's ArrayList.

Sorin Mocanu
A: 

There are many ways to create a Set and a List. HashSet and ArrayList are just two examples. It is also fairly common to use generics with collections these days. I suggest you have a look at what they are

This is a good introduction for java's builtin collections. http://java.sun.com/javase/6/docs/technotes/guides/collections/overview.html

Peter Lawrey
+1  A: 

Using Google Collections, you could use the following methods in the Lists class

import com.google.common.collect.Lists;

// ...

List<String> strings = Lists.newArrayList();

List<Integer> integers = Lists.newLinkedList();

There are overloads for varargs initialization and initialising from an Iterable<T>.

The advantage of these methods is that you don't need to specify the generic parameter explicitly as you would with the constructor - the compiler will infer it from the type of the variable.

Ben Lings