Java Collection Classes



General Information

Introduction

Interfaces: List---ordered, Set---no duplicates, Map---key/value pairs.

resizable arraylinked list

hash tablebalanced tree

HashMap: a hashtable. HashSet: a set implemented using a hashtable.

TreeSet: ordered set implemented using red-black trees. TreeMap: hashtable kept in ascending order of keys and implemented using red-black trees.

A number of algorithms are found in the class (not to be confused with the interface ): sorting, shuffling, searching, extremes, reverse, copy, fill.

  static int binarySearch (List list, Object key, Comparator comp)
  static void fill (List list, Object o)
  static Object min (Collection coll, Comparator comp)
  static Object max (Collection coll, Comparator comp) 
  static void reverse (List l)
  static void shuffle (List list, Random rnd)
  static void sort (List list, Comparator c) 
  static void copy (List dest, List src)
  static void rotate (List list, int distance)               // j2se 1.4
  static boolean replaceAll (List list, Object oldVal, Object newVal)  // j2se 1.4
  

Tip. Write methods that take the interfaces as arguments. Don't use Set or List, when Collection will do.

Idiom for stepping through a collection:

  for (Iterator i=collection.iterator(); i.hasNext(); /**/) {
     final TYPE x = (TYPE) i.next();
     // Use x
  }
  

In Java 1.5.

  for (TYPE x : collection) {
     // Use x
  }
  

Idiom for stepping through all the key-value pairs in a Map:

  for (Iterator i=map.entrySet().iterator(); i.hasNext(); /**/) {
     final Map.Entry e = (Map.Entry) i.next();
     System.out.println (e.getKey() + ": " + e.getValue());
  }
  for (Map.Entry e: map.entrySet()) {
     System.out.println (e.getKey() + ": " + e.getValue());
  }
  

If you have an array of objects, you can convert it to a collection.

  public static List Arrays.asList (Object [] arr);

  List l = Arrays.asList (new String [] { "dogs", "cats"});
  
If you have an array of primitive types (e.g., int, double), then there is no direct way of converting it to a collection. You will have you use a loop and add each element to the collection individually. Actually, you cannot have a collection of primitive elements, so an appropriate object will have to be used instead---one of the wrapper classes for the primitive types might be useful in this case.

If you have a collection, you can convert to an array of objects.

  Object [] toArray ();

  Object [] arr = c.toArray ();
  

Example

An example using several of the collection classes to solve a complex problem.

Wrapper Implementations

A number of static, utility methods are found in the class java.util.Collections (not to be confused with the interface java.util.Collection). These methods provide collections with two additional properties: safe multi-thread access, and immutability.

Synchronized

public static Collection synchronizedCollection(Collection c);
public static Set synchronizedSet(Set s);
public static List synchronizedList(List list);
public static Map synchronizedMap(Map m);
public static SortedSet synchronizedSortedSet(SortedSet s);
public static SortedMap synchronizedSortedMap(SortedMap m);
  

Immutable

Good for passing a collection to someone and ensuring they don't change it. Attempts to change it, eg., add, raise UnsupportedOperationException.

public static Collection unmodifiableCollection(Collection c);
public static Set unmodifiableSet(Set s);
public static List unmodifiableList(List list);
public static Map unmodifiableMap(Map m);
public static SortedSet unmodifiableSortedSet(SortedSet s);
public static SortedMap unmodifiableSortedMap(SortedMap m);

Iterators

Important points about iteration
Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Wed May 11 08:19:52 EDT 2005