how2examples.com
Home - Introduction to Java Programming

The Java Collections API

Collections are used for storing groups of objects. The Collections API provides a number of interfaces (including Collection, List, Map and Set) to define a standard way of using a range of concrete data structures. The interfaces and classes of the Collections API belong to the java.util package.

Commonly used classes and interfaces of Java's Collection framework
Commonly used classes and interfaces of Java's Collection framework
(Click image to see larger version.)

Collection Characteristics

Characteristics of collections include:

The java.util.List interface

A list is an ordered collection. Lists allow precise control over the positioning of individual elements within the data structure. It is possible to add, retrieve and remove elements at specified positions. The standard behaviour for lists is to allow duplicates. Implementations of List include:

Contents of LinkedListExample.java:
import java.util.LinkedList;

public class LinkedListExample {
   public static void main(String[] args) {
      LinkedList<String> l = new LinkedList<String>();
      System.out.println(l.size() + " " + l.isEmpty());
      l.add("a");
      l.add("b");
      l.add("c");
      l.add("d");
      l.add("e");
      l.add("f");
      System.out.println(l);
      System.out.println(l.set(1, "x"));
      l.add(2, "a");
      System.out.println(l);
      System.out.println(l.pollFirst() + " " + l.peekFirst());
      System.out.println(l);
      System.out.println(l.get(4));
      l.remove("a");
      System.out.println(l);
      System.out.println(l.size() + " " + l.isEmpty());
      for (String s : l) {
         System.out.print(s);
      }
   }
}
Command to compile LinkedListExample.java:
javac LinkedListExample.java
Command to run LinkedListExample:
java LinkedListExample
Output from running LinkedListExample:
0 true
[a, b, c, d, e, f]
b
[a, x, a, c, d, e, f]
a x
[x, a, c, d, e, f]
e
[x, c, d, e, f]
5 false
xcdef

The java.util.Set interface

A set does not allow duplicates. Implementations of Set include:

Contents of LinkedHashSetExample.java:
import java.util.LinkedHashSet;

public class LinkedHashSetExample {
   public static void main(String[] args) {
      LinkedHashSet<String> s = new LinkedHashSet<String>();
      System.out.println(s.size() + " " + s.isEmpty());
      System.out.print(s.add("value1"));
      System.out.print(s.add("value2"));
      System.out.print(s.add("value3"));
      System.out.print(s.add("value1"));
      System.out.println();
      System.out.println(s);
      System.out.println(s.contains("value3") + " " + s.contains("value9"));
      System.out.println(s.remove("value2") + " " + s.remove("value2"));
      System.out.println(s);
      System.out.println(s.size() + " " + s.isEmpty());
      for (String v : s) {
         System.out.print(v);
      }
   }
}
Command to compile LinkedHashSetExample.java:
javac LinkedHashSetExample.java
Command to run LinkedHashSetExample:
java LinkedHashSetExample
Output from running LinkedHashSetExample:
0 true
truetruetruefalse
[value1, value2, value3]
true false
true false
[value1, value3]
2 false
value1value3

The java.util.Map interface

Maps map keys to values. When an object is added to a map it is associated with the specified key. A map cannot contain duplicate keys. A key can only be associated with one value at a time. Implementations of Map include:

Contents of LinkedHashMapExample.java:
import static java.util.Map.Entry;
import java.util.LinkedHashMap;

public class LinkedHashMapExample {
   public static void main(String[] args) {
      LinkedHashMap<String, String> m = new LinkedHashMap<String, String>();
      System.out.println(m.size() + " " + m.isEmpty());
      System.out.print(m.put("key1", "value1"));
      System.out.print(m.put("key2", "value2"));
      System.out.print(m.put("key3", "value3"));
      System.out.print(m.put("key3", "value1"));
      System.out.println();
      System.out.println(m);
      System.out.println(m.containsKey("key2") + " " + m.containsValue("key2"));
      System.out.println(m.containsKey("value1") + " " + m.containsValue("value1"));
      System.out.println(m.get("key2") + " " + m.get("key9"));
      System.out.println(m.remove("key1") + " " + m.remove("key1"));
      System.out.println(m);
      System.out.println(m.size() + " " + m.isEmpty());
      for (String k : m.keySet()) {
         System.out.print(k);
      }
      System.out.println();
      for (String v : m.values()) {
         System.out.print(v);
      }
      System.out.println();
      for (Entry<String, String> e : m.entrySet()) {
         System.out.print("[" + e.getKey() + " " + e.getValue() + "]");
      }
   }
}
Command to compile LinkedHashMapExample.java:
javac LinkedHashMapExample.java
Command to run LinkedHashMapExample:
java LinkedHashMapExample
Output from running LinkedHashMapExample:
0 true
nullnullnullvalue3
{key1=value1, key2=value2, key3=value1}
true false
false true
value2 null
value1 null
{key2=value2, key3=value1}
2 false
key2key3
value2value1
[key2 value2][key3 value1]

The java.util.Collections class

The java.util.Collections class contains static factory and helper methods for working with collections. Using the methods provided by Collections avoids developers having to spend time writing their own, possibly inefficient or incorrect, algorithms to perform common operations such as populating, sorting and searching of collections.

Contents of CollectionsExample.java:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsExample {
   public static void main(String[] args) {
      String[] a = new String[]{"apple", "orange", "banana", "kiwi", "banana", "grape"};
      List<String> l = new ArrayList<String>();
      Collections.addAll(l, a);
      System.out.println(l);
      System.out.println(Collections.frequency(l, "banana"));
      System.out.println(Collections.min(l));
      System.out.println(Collections.max(l));
      Collections.swap(l,0,2);
      System.out.println(l);
      Collections.reverse(l);
      System.out.println(l);
      Collections.sort(l);
      System.out.println(l);
      
      List<String> unmodifiable = Collections.unmodifiableList(l);
      for (String v : unmodifiable) {
         System.out.print(v);
      }
      System.out.println();
      try {
         unmodifiable.add("pear");
      } catch (UnsupportedOperationException e) {
         e.printStackTrace();
      }
   }
}
Command to compile CollectionsExample.java:
javac CollectionsExample.java
Command to run CollectionsExample:
java CollectionsExample
Output from running CollectionsExample:
[apple, orange, banana, kiwi, banana, grape]
2
apple
orange
[banana, orange, apple, kiwi, banana, grape]
[grape, banana, kiwi, apple, orange, banana]
[apple, banana, banana, grape, kiwi, orange]
applebananabananagrapekiwiorange
java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableCollection.add(Collections.java:1018)
   at CollectionsExample.main(CollectionsExample.java:27)

See Also