Skip to content

Java Collections Library


Overview

  • The Collections Interfaces
  • We’ll be focusing on these interfaces.
    • Set
    • SortedSet
    • List
    • Map
    • SortedMap

Definitions

Set

  • a collection that cannot contain duplicate elements.
  • a collection that does not guarantee the order of its elements.

SortedSet

  • a collection that cannot contain duplicate elements.
  • a collection that is continuously sorted by its elements natural order or by a custom order.

List

  • a collection that can contain duplicate elements.
  • a collection that maintains and guarantees the order of its elements.

Map

  • a collection that maps keys to values.
  • a map cannot contain duplicate keys.
  • a key can map to one and only one value.
  • a map can contain duplicate values.
  • a map’s keys are not in a predictable or guaranteed order.

SortedMap

  • a collection that maps keys to values.
  • a sorted map cannot contain duplicate keys.
  • a key can map to one and only one value.
  • a sorted map can contain duplicate values.
  • a sorted map’s keys are continuously sorted by its elements natural order or by a custom order.

API

  • Java API
  • The primary methods for the Collection interface
    • boolean add(E e)
    • boolean contains(Object o)
    • Iterator<E> iterator()
    • int size()
  • The primary methods for the Map interface.
    • boolean containsKey(Object key)
    • Set<Map.Entry<K,V>\> entrySet()
    • V get(Object key)
    • Set<K> keySet()
    • V put(K key, V value)
    • int size()

Concrete Implementations

Set => HashSet

  • A normal set that has no predictable order

SortedSet => TreeSet

  • A sorted set whose elements are sorted in their natural order.

List => ArrayList

  • A normal list that maintains the order in which the elements were added.

Map => HashMap

  • A normal map whose keys are not ordered.

SortedMap => TreeMap

  • A sorted map whose keys are sorted in their natural order.

Lists

Create a List

    // Java 4
        List list = new ArrayList();

    // Java 5-9
        List<String> list = new ArrayList<String>();

Notice the type for the list? It’s a “best practice” to declare variables as an interface whenever possible. Why?

    // If the compiler can infer the type we can just use "<>"
    List<String> list = new ArrayList<>();

Add to a List

The add method is used to add to a list.

List<String> list = new ArrayList<>();

list.add("one");
list.add("two");

Display a List

Getting a quick view of a List.

System.out.println(list);

Output

[one, two]
  • The easiest way to see the contents of a list that’s not too big, is to simply use a System.out.println() with the list reference variable as the argument.
  • Question? With this technique, what method is called on each element in list?

Iterating through a List

// Iteration the java 1.4 way.

    System.out.println("Iteration the java 1.4 way");    
    for (Iterator iterator = list.iterator(); iterator.hasNext();){
        System.out.println(iterator.next());
    }
// Iteration the java 5-9 way. We will use this!

    System.out.println("Iteration the java 5-9 way");
    for (String element : list){
        System.out.println(element);
    }

Output

Iteration the java 1.4 way
one
two

Iteration the java 5-9 way
one
two

Complete demo class

package java112.labs1;

import java.util.*;

/**
 * @author Eric Knapp
 * class ListDemo
 *
 */
public class ListDemo {

    public void run() {

        List<String> list = new ArrayList<>();

        list.add("one");
        list.add("two");

        System.out.println(list);
        System.out.println();

        System.out.println("Iteration the java 1.4 way");
        for (Iterator iterator = list.iterator(); iterator.hasNext();){
            System.out.println(iterator.next());
        }
        System.out.println();

        System.out.println("Iteration the java 5-9 way");
        for (String element : list){
            System.out.println(element);
        }

    }

    public static void main(String[] args) {
        ListDemo demo = new ListDemo();
        demo.run();
    }
}

Lab time!

Lab 7 - First List

Sets

Create a Set

// Java 4
    Set set = new HashSet();

// Java 5-9
    Set<String> set = new HashSet<>();

Add to a Set

// What happens to the duplicates?
    set.add("one");
    set.add("one");
    set.add("two");
    set.add("two");
    set.add("two");
    set.add("two");
    set.add("three");
    set.add("three");

Display a Set

Getting a quick view of a Set.

// Just like a list
    System.out.println(set);

Output

[one, three, two]

Hmm, the dups are gone!

Iterating through a Set

// Iteration the java 1.4 way

    System.out.println("Iteration the java 1.4 way");
    for (Iterator iterator = set.iterator(); iterator.hasNext();){
        System.out.println(iterator.next());
    }
// Iteration the java 5-9 way. This is the way we'll do it!

    System.out.println("Iteration the java 5-8 way");
    for (String element : set){
        System.out.println(element);
    }

Output

Iteration the java 1.4 way
one
three
two

Iteration the java 5-8 way
one
three
two

Complete Set demo

package java112.labs1;

import java.util.*;

/**
 * @author Eric Knapp
 * class SetDemo
 *
 */
public class SetDemo {

    public void run() {

        Set<String> set = new TreeSet<>();

        set.add("one");
        set.add("one");
        set.add("two");
        set.add("two");
        set.add("two");
        set.add("two");
        set.add("three");
        set.add("three");

        System.out.println(set);
        System.out.println();

        System.out.println("Iteration the java 1.4 way");
        for (Iterator iterator = set.iterator(); iterator.hasNext();){
            System.out.println(iterator.next());
        }
        System.out.println();

        System.out.println("Iteration the java 5-8 way");
        for (String element : set){
            System.out.println(element);
        }

    }

    public static void main(String[] args) {
        SetDemo demo = new SetDemo();
        demo.run();
    }
}

Another Lab!

Lab 8 - First Set