Java.Util.List is an ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.
The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.
The List interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.
The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.
Creating a List, Insert, Retrieve and Remove
// Create the list
List list = new LinkedList(); // Doubly-linked list
list = new ArrayList(); // List implemented as growable array
// Append an element to the list
list.add("a");
// Insert an element at the head of the list
list.add(0, "b");
// Get the number of elements in the list
int size = list.size(); // 2
// Retrieving the element at the end of the list
Object element = list.get(list.size()-1); // a
// Retrieving the element at the head of the list
element = list.get(0); // b
// Remove the first occurrence of an element
boolean b = list.remove("b"); // true
b = list.remove("b"); // false
// Remove the element at a particular index
element = list.remove(0); // a
Operating on Lists
// Create the lists
List list1 = new ArrayList();
List list2 = new ArrayList();
// Add elements to the lists ...
// Copy all the elements from list2 to list1 (list1 += list2)
// list1 becomes the union of list1 and list2
list1.addAll(list2);
// Remove all the elements in list1 from list2 (list1 -= list2)
// list1 becomes the asymmetric difference of list1 and list2
list1.removeAll(list2);
// Get the intersection of list1 and list2
// list1 becomes the intersection of list1 and list2
list1.retainAll(list2);
// Remove all elements from a list
list1.clear();
// Truncate the list
int newSize = 2;
list1.subList(newSize, list1.size()).clear();
Sorting a List
// Create a list
String[] strArray = new String[] {"z", "a", "C"};
List list = Arrays.asList(strArray);
// Sort
Collections.sort(list);
// C, a, z
// Case-insensitive sort
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
// a, C, z
// Reverse-order sort
Collections.sort(list, Collections.reverseOrder());
// z, a, C
// Case-insensitive reverse-order sort
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(list);
// z, C, a
Convert array to java.util.list
Object[] array = new Object[]{"12","23","34"};
java.util.List list = Arrays.asList(array);
Creating a Type-Specific List [JvaSE 5.0 +]
Generics can be used to create a list that will hold only objects of a certain type. This example creates a list of Integer objects.
List<Integer> intlist = new ArrayList<Integer>();
intlist.add(new Integer(123));
// intlist.add("hello"); <- Syntax error
A list declared to hold objects of a type T can also hold objects that extend from T. In this example, a list is created to hold Number objects. Both Integer and Float are subclasses of Number.
List<Number> numlist = new ArrayList<Number>();
numlist.add(new Integer(123));
numlist.add(new Float(123));
Note that although null is not a subclass of any type, if the collection supports null values, it can be added to the type-specific collection.
intlist.add(null);
A value retrieved from a type-specific list does not need to be casted. In this example, a URL value is retrieved and used without an explicit cast.
List<URL> urlList = new ArrayList<URL>();
try {
urlList.add(new URL("http://exampledepot.com/"));
} catch (MalformedURLException e) {
}
String s = urlList.get(0).getHost();