The java.util.List interface is a subtype of the java.util.Collection interface. It represents an ordered list of objects, meaning you can access the elements of a List in a specific order, and by an index too. You can also add the same element more than once to a List.
Here is a list of the topics covered in this text:
  1. List Implementations
  2. Adding and Accessing Elements
  3. Removing Elements
  4. Generic Lists
  5. More Details in the JavaDoc

List Implementations

Being a Collection subtype all methods in the Collection interface are also available in theList interface.
Since List is an interface you need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following List implementations in the Java Collections API:
  • java.util.ArrayList
  • java.util.LinkedList
  • java.util.Vector
  • java.util.Stack
There are also List implementations in the java.util.concurrent package, but I will leave the concurrency utilities out of this tutorial.
Here are a few examples of how to create a List instance:
List listA = new ArrayList();
List listB = new LinkedList();
List listC = new Vector();
List listD = new Stack();    

Adding and Accessing Elements

To add elements to a List you call its add() method. This method is inherited from theCollection interface. Here are a few examples:
List listA = new ArrayList();

listA.add("element 1");
listA.add("element 2");
listA.add("element 3");

listA.add(0, "element 0");
The first three add() calls add a String instance to the end of the list. The last add() call adds aString at index 0, meaning at the beginning of the list.
The order in which the elements are added to the List is stored, so you can access the elements in the same order. You can do so using either the get(int index) method, or via the Iteratorreturned by the iterator() method. Here is how:
List listA = new ArrayList();

listA.add("element 0");
listA.add("element 1");
listA.add("element 2");

//access via index
String element0 = listA.get(0);
String element1 = listA.get(1);
String element3 = listA.get(2);


//access via Iterator
Iterator iterator = listA.iterator();
while(iterator.hasNext(){
  String element = (String) iterator.next();
}


//access via new for-loop
for(Object object : listA) {
    String element = (String) object;
}
When iterating the list via its Iterator or via the for-loop (which also uses the Iterator behind the scene), the elements are iterated in the same sequence they are stored in the list.

Removing Elements

You can remove elements in two ways:
  1. remove(Object element)
  2. remove(int index)
remove(Object element) removes that element in the list, if it is present. All subsequent elements in the list are then moved up in the list. Their index thus decreases by 1.
remove(int index) removes the element at the given index. All subsequent elements in the list are then moved up in the list. Their index thus decreases by 1.

Generic Lists

By default you can put any Object into a List, but from Java 5, Java Generics makes it possible to limit the types of object you can insert into a List. Here is an example:
List list = new ArrayList();
This List can now only have MyObject instances inserted into it. You can then access and iterate its elements without casting them. Here is how it looks:
MyObject myObject = list.get(0);

for(MyObject anObject : list){
   //do someting to anObject...   
}

More Details in the JavaDoc

There is a lot more you can do with a List, but you will have to check out the JavaDoc for more details. This text focused on the two most common operations: Adding / removing elements, and iterating the elements.

- Copyright © 2013 Taqi Shah Blogspot -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -