Horstmann Chapter 15

Big Java 4

Chapter 15 –The Java Collections Framework

Chapter Goals

a collection of stamps

  • To learn how to use the collection classes supplied in the Java library
  • To use iterators to traverse collections
  • To choose appropriate collections for solving programming problems
  • To study applications of stacks and queues

An Overview of the Collections Framework

  • A collection groups together elements and allows them to be retrieved later.
  • Java collections framework: a hierarchy of interface types and classes for collecting objects.
    • Each interface type is implemented by one or more classes

  • Collection Hierarchy

    Figure 1 Interfaces and Classes in the Java Collections Framework

  • The Collection interface is at the root
    • All Collection class implement this interface
    • So all have a common set of methods

An Overview of the Collections Framework

  • List interface
  • A list is a collection that remembers the order of its elements.
  • Two implementing classes
    • ArrayList
    • LinkedList
  • a list of books

    Figure 2 A List of Books

An Overview of the Collections Framework

  • Set interface
  • A set is an unordered collection of unique elements.
  • Arranges its elements so that finding, adding, and removing elements is more efficient.
  • Two mechanisms to do this
    • hash tables
    • binary search trees
  • set of books

    Figure 3 A Set of Books

An Overview of the Collections Framework

  • Stack
    • Remembers the order of elements
    • But you can only add and remove at the top
  • stack of books

    Figure 4 A Stack of Books

An Overview of the Collections Framework

  • Queue
    • Add items to one end (the tail) and remove them from the other end (the head)
  • A queue of People
    people in a queue
  • A priority queue
    • an unordered collection
    • has an efficient operation for removing the element with the highest priority

An Overview of the Collections Framework

  • Map
    • Keeps associations between key and value objects.
    • Every key in the map has an associated value.
    • The map stores the keys, values, and the associations between them.
  • shows association between barcodes and books

    Figure 5 A Map from Bar Codes to Books

An Overview of the Collections Framework

  • Every class that implements the Collection interface has these methods.
    Collection interface methods

Self Check 15.1

A gradebook application stores a collection of quizzes. Should it use a list or a set?
  • Answer: A list is a better choice because the application will want to retain the order in which the quizzes were given.

Self Check 15.2

A student information system stores a collection of student records for a university. Should it use a list or a set?
  • Answer: A set is a better choice. There is no intrinsically useful ordering for the students. For example, the registrar's office has little use for a list of all students by their GPA. By storing them in a set, adding, removing, and finding students can be efficient.

Self Check 15.3

Why is a queue of books a better choice than a stack for organizing your required reading?
  • Answer: With a stack, you would always read the latest required reading, and you might never get to the oldest readings.

Self Check 15.4

As you can see from Figure 1, the Java collections framework does not consider a map a collection. Give a reason for this decision.
  • Answer: A collection stores elements, but a map stores associations between elements.

Linked Lists

  • A data structure used for collecting a sequence of objects:
    • Allows efficient addition and removal of elements in the middle of the sequence.
  • A linked list consists of a number of nodes;
    • Each node has a reference to the next node.
  • A node is an object that stores an element and references to the neighboring nodes.
  • Each node in a linked list is connected to the neighboring nodes.
    people linked by holding hands

Linked Lists

  • Adding and removing elements in the middle of a linked list is efficient.
  • Visiting the elements of a linked list in sequential order is efficient.
  • Random access is not efficient.
  • a linked list

    Figure 6 Example of a linked list

Linked Lists

  • When inserting or removing a node:
    • Only the neighboring node references need to be updated
  • inserting into a linked list

    Figure 7 Inserting a Node into a Linked List

    removing a node from a linked list

    Figure 8 Removing a Node From A Linked List

  • Visiting the elements of a linked list in sequential order is efficient.
  • Random access is not efficient.

Linked Lists

  • When to use a linked list:
    • You are concerned about the efficiency of inserting or removing elements
    • You rarely need element access in random order

The LinkedList Class of the Java Collections Framework

  • Generic class
    • Specify type of elements in angle brackets: LinkedList<Product>
  • Package: java.util
  • LinkedList has the methods of the Collection interface.
  • Some additional LinkedList methods:
    LinkedList methods

List Iterator

  • Use a list iterator to access elements inside a linked list.
  • Encapsulates a position anywhere inside the linked list.
  • Think of an iterator as pointing between two elements:
    • Analogy: like the cursor in a word processor points between two characters
  • To get a list iterator, use the listIterator method of the LinkedList class.
    LinkedList<String> employeeNames = . . .;
    ListIterator<String> iterator = employeeNames.listIterator();
  • Also a generic type.

List Iterator

  • Initially points before the first element.
  • Move the position with next method:
    if (iterator.hasNext())
  • The next method returns the element that the iterator is passing.
  • The return type of the next method matches the list iterator's type parameter.

List Iterator

  • To traverse all elements in a linked list of strings:
    while (iterator.hasNext())
       String name = iterator.next();
       Do something with name
  • To use the “for each” loop:
    for (String name : employeeNames)
       Do something with name


List Iterator

  • The nodes of the LinkedList class store two links:
    • One to the next element
    • One to the previous one
    • Called a doubly-linked list
  • To move the list position backwards, use:
    • hasPrevious
    • previous

A List Iterator

  • The add method adds an object after the iterator.
    • Then moves the iterator position past the new element.
Conceptual view og list iterator

Figure 8 A Conceptual View of the List Iterator

List Iterator

  • The remove method:
    • Removes object that was returned by the last call to next or previous
  • To remove all names that fulfill a certain condition:
    while (iterator.hasNext())
       String name = iterator.next();
       if (condition is fulfilled for name)
  • Be careful when calling remove:
    • It can be called only once after calling next or previous
    • You cannot call it immediately after a call to add
    • If you call it improperly, it throws an IllegalStateException

List Iterator

  • ListIterator interface extends Iterator interface.
  • Methods of the Iterator and ListIterator Interfaces
    list iterator methods

Sample Program

  • ListDemo is a sample program that:
    • Inserts strings into a list
    • Iterates through the list, adding and removing elements
    • Prints the list


Your browser does not support the <object> tag. Program Run:
  • [Diana Harry Juliet Nina Tom]
    Expected: [Diana Harry Juliet Nina Tom]

Self Check 15.5

Do linked lists take more storage space than arrays of the same size?
  • Answer: Yes, for two reasons. A linked list needs to store the neighboring node references, which are not needed in an arry. Moreover, there is some overhead for storing an object. In a linked list, each node is a separate object that incurs this overhead, whereas an array is a single object.

Self Check 15.6

Why don't we need iterators with arrays?
  • Answer: We can simply access each array element with an integer index.

Self Check 15.7

Suppose the list letters contains elements "A", "B", "C", and "D". Draw the contents of the list and the iterator position for the following operations:
ListIterator<String> iter = letters.iterator();
  • Answer:

Self Check 15.8

Write a loop that removes all strings with length less than four from a linked list of strings called words.
  • Answer:
    ListIterator<String> iter = words.iterator();
    while (iter.hasNext())
       String str = iter.next();
       if (str.length() < 4) { iter.remove(); }

Self Check 15.9

Write a loop that prints every second element of a linked list of strings called words.
  • Answer:
    ListIterator<String> iter = words.iterator();
    while (iter.hasNext())
       if (iter.hasNext())
          iter.next(); // Skip the next element


  • A set organizes its values in an order that is optimized for efficiency.
  • May not be the order in which you add elements.
  • Inserting and removing elements is more efficient with a set than with a list.


  • The Set interface has the same methods as the Collection interface.
  • A set does not admit duplicates.
  • Two implementing classes
    • HashSet
      • based on hash table
    • TreeSet
      • based on binary search tree
  • A Set implementation arranges the elements so that it can locate them quickly.


  • In a hash table
    • Set elements are grouped into smaller collections of elements that share the same characteristic.
      • Grouped by an integer hash code
      • Computed from the element
  • Elements in a hash table must implement the method hashCode.
  • Must have a properly defined equals method.
  • You can form hash sets holding objects of type String, Integer, Double, Point, Rectangle, or Color.
    • HashSet<String>, HashSet<Rectangle>, or a HashSet<HashSet<Integer>>
  • On this shelf, books of the same color are grouped together. Similarly, in a hash table, objects with the same hash code are placed in the same grou.p
    set of books


  • In a TreeSet
    • Elements are kept in sorted order
      tree set of books
  • Elements are stored in nodes.
  • The nodes are arranged in a tree shape,
    • Not in a linear sequence
  • You can form tree sets for any class that implements the Comparable interface:
    • Example: String or Integer.


  • Use a TreeSet if you want to visit the set's elements in sorted order.
    • Otherwise choose a HashSet
      • It is a bit more efficient — if the hash function is well chosen


  • Store the reference to a TreeSet or HashSet in a Set<String> variable:
       Set<String> names = new HashSet<String>();
       Set<String> names = new TreeSet<String>();
  • After constructing the collection object:
    • The implementation no longer matters
    • Only the interface is important

Working with Sets

  • Adding and removing elements:
  • Sets don't have duplicates.
    • Adding a duplicate is ignored.
  • Attempting to remove an element that isn't in the set is ignored.
  • The contains method tests whether an element is contained in the set:
    if (names.contains("Juliet")) . . .
    • The contains method uses the equals method of the element type

Working with Sets

  • To process all elements in the set, get an iterator.
  • A set iterator visits the elements in the order in which the set implementation keeps them.
    Iterator<String> iter = names.iterator();
    while (iter.hasNext())
       String name = iter.next();
       Do something with name
  • You can also use the “for each” loop
    for (String name : names)
       Do something with name
  • You cannot add an element to a set at an iterator position - A set is unordered.
  • You can remove an element at an iterator position.
  • The iterator interface as no previous method.

Working with Sets

Methods for working with sets

SpellCheck Example Program

  • Read all the correctly spelled words from a dictionary file
    • Put them in a set
  • Reads all words from a document
    • Put them in a second set
  • Print all the words in the second set that are not in the dictionary set.
  • Potential misspellings


Your browser does not support this feature Program Run:
  • neighbouring

Self Check 15.10

Arrays and lists remember the order in which you added elements; sets do not. Why would you want to use a set instead of an array or list?
  • Answer: Adding and removing elements as well as testing for membership is more efficient with sets.

Self Check 15.11

Why are set iterators different from list iterators?
  • Answer: Sets do not have an ordering, so it doesn't make sense to add an element at a particular iterator position, or to traverse a set backward.

Self Check 15.12

What is wrong with the following test to check whether the Set<String>s contains the elements "Tom", "Diana", and "Harry"?
if (s.toString().equals("[Tom, Diana, Harry]")) . . .
  • Answer: You do not know in which order the set keeps the elements.

Self Check 15.13

How can you correctly implement the test of Self Check 12?
  • Answer: Here is one possibility:
    if (s.size() == 3 && s.contains("Tom")
         && s.contains("Diana")
         && s.contains("Harry"))
       . . .

Self Check 15.14

Write a loop that prints all elements that are in both Set<String>s and Set<String>t.
  • Answer:
    for (String str : s)
       if (t.contains(str))

Self Check 15.15

Suppose you changed line 40 of the SpellCheck program to use a TreeSet instead of a HashSet. How would the output change?
  • Answer: The words would be listed in sorted order.


  • A map allows you to associate elements from a key set with elements from a value collection.
  • Use a map when you want to look up objects by using a key.
    A map og people to colors
  • Figure 10 A Map

  • Two implementations of the Map interface:
    • HashMap
    • TreeMap
  • Store the reference to the map object in a Map reference:
    Map<String, Color> favoriteColors = new HashMap<String, Color>();


  • Use the put method to add an association:
    favoriteColors.put("Juliet", Color.RED);
  • You can change the value of an existing association by calling put again:
    favoriteColors.put("Juliet", Color.BLUE);
  • The get method returns the value associated with a key:
     Color favorite = favorite("Juliet");
  • If you ask for a key that isn't associated with any values, the get method returns null.
  • To remove an association, call the remove method with the key:
  • Working with Maps

    map methods


    • Sometimes you want to enumerate all keys in a map.
    • The keySet method yields the set of keys.
    • Ask the key set for an iterator and get all keys.
    • For each key, you can find the associated value with the get method.
    • To print all key/value pairs in a map m:
      Set<String> keySet = m.keySet();
      for (String key : keySet)
          Color value = m.get(key);
          System.out.println(key + "->" + value);


    Your browser does not support this feature Program Run:
    • Juliet : java.awt.Color[r=0,g=0,b=255]
      Adam : java.awt.Color[r=255,g=0,b=0]
      Eve : java.awt.Color[r=0,g=0,b=255]
      Romeo : java.awt.Color[r=0,g=255,b=0]

    Self Check 15.16

    What is the difference between a set and a map?
    • Answer: A set stores elements. A map stores associations between keys and values.

    Self Check 15.17

    Why is the collection of the keys of a map a set and not a list?
    • Answer: The ordering does not matter, and you cannot have duplicates

    Self Check 15.18

    Why is the collection of the values of a map not a set?
    • Answer: Because it might have duplicates.

    Self Check 15.19

    Suppose you want to track how many times each word occurs in a document. Declare a suitable map variable.
    • Answer: Map<String, Integer> wordFrequency;

    Self Check 15.20

    What is a Map<String, HashSet<String>>? Give a possible use for such a structure.
    • Answer: It associates strings with sets of strings. One application would be a thesaurus that lists synonyms for a given word. For example, the key "improve" might have as its value the set ["ameliorate", "better", "enhance", "enrich", "perfect", "refine"].

    Choosing a Collection

    1. Determine how you access the values.
    2. Determine the element types or key/value types.
    3. Determine whether element or key order matters.
    4. For a collection, determine which operations must be efficient.
    5. For hash sets and maps, decide whether you need to implement the hashCode and equals methods.
    6. If you use a tree, decide whether to supply a comparator.

    Hash Functions

    • You may need to implement a hash function for your own classes.
    • A hash function: a function that computes an integer value, the hash code, from an object in such a way that different objects are likely to yield different hash codes.
    • Object class has a hashCode method
      • you need to override it to use your class in a hash table
    • A collision: two or more objects have the same hash code.
    • The method used by the String class to compute the hash code.
      final int HASH_MULTIPLIER = 31;
      int h = 0;
      for (int i = 0; i < s.length(); i++)
         h = HASH_MULTIPLIER * h + s.charAt(i);
    • This produces different hash codes for "tea" and "eat".

    Hash Functions

    spreading seeds around


    A good hash function produces different hash values for each object so that they are scattered about in a hash table.

    Hash Functions

    • Override hashCode methods in your own classes by combining the hash codes for the instance variables.
    • A hash function for our Country class:
      public class Country
         public int hashCode()
            int h1 = name.hashCode();
            int h2 = new Double(area).hashCode();
            final int HASH_MULTIPLIER = 29;
            int h = HASH_MULTIPLIER * h1 + h2;
            return h;
    • A class's hashCode method must be compatible with its equals method.


    • A stack lets you insert and remove elements only at one end:
      • Called the top of the stack.
      • Removes items in the opposite order than they were added
      • Last-in, first-out or LIFO order
    • Add and remove methods are called push and pop.
    • Example
      Stack<String> s = new Stack<String>();
      s.push("A"); s.push("B"); s.push("C");
      while (s.size() > 0)
         System.out.print(s.pop() + " "); // Prints C B A
    • The last pancake that has been added to this stack will be the first one that is consumed.
      stack of pancakes


    • Many applications for stacks in computer science.
    • Consider: Undo function of a word processor
      • The issued commands are kept in a stack.
      • When you select “Undo”, the last command is popped off the stack and undone
    • Run-time stack that a processor or virtual machine:
      • Stores the values of variables in nested methods.
      • When a new method is called, its parameter variables and local variables are pushed onto a stack.
      • When the method exits, they are popped off again.

    Stack in the Java Library

    • Stack class provides push, pop and peek methods.
      stack methods


    • A queue
      • Lets you add items to one end of the queue (the tail)
      • Remove items from the other end of the queue (the head)
      • Items are removed in the same order in which they were added
      • First-in, first-out or FIFO order
    • To visualize a queue, think of people lining up.
      line of people
    • Typical application: a print queue.


    • The Queue interface in the standard Java library has:
      • an add method to add an element to the tail of the queue,
      • A remove method to remove the head of the queue, and
      • A peek method to get the head element of the queue without removing it.
    • The LinkedList class implements the Queue interface.
    • When you need a queue, initialize a Queue variable with a LinkedList object:
       Queue<String> q = new LinkedList<String>();
       q.add("A"); q.add("B"); q.add("C");
       while (q.size() > 0) { System.out.print(q.remove() + " "); } // Prints A B C
    • queue methods

    Priority Queues

    • A priority queue collects elements, each of which has a priority.
    • Example: a collection of work requests, some of which may be more urgent than others.
    • Does not maintain a first-in, first-out discipline.
    • Elements are retrieved according to their priority.
    • Priority 1 denotes the most urgent priority
      • Each removal extracts the minimum element
    • When you retrieve an item from a priority queue, you always get the most urgent one.

    Priority Queues

    • Example: objects of a class WorkOrder into a priority queue.
      PriorityQueue<WorkOrder> q = new PriorityQueue<WorkOrder>();
      q.add(new WorkOrder(3, "Shampoo carpets"));
      q.add(new WorkOrder(1, "Fix broken sink"));
      q.add(new WorkOrder(2, "Order cleaning supplies"));
    • When calling q.remove() for the first time, the work order with priority 1 is removed.
    • Elements should belong to a class that implements the Comparable interface.
    • priority queue methods

    Self Check 15.21

    Why would you want to declare a variable as
    Queue<String> q = new LinkedList<String>()
    instead of simply declaring it as a linked list?
    • Answer: This way, we can ensure that only queue operations can be invoked on the q object.

    Self Check 15.22

    Why wouldn't you want to use an array list for implementing a queue?
    • Answer: Depending on whether you consider the 0 position the head or the tail of the queue, you would either add or remove elements at that position. Both are inefficient operations because all other elements need to be moved.

    Self Check 15.23

    What does this code print?
    Queue<String> q = new LinkedList<String>();
    while (q.size() > 0) { System.out.print(q.remove() + " "); }
    • Answer: A B C

    Self Check 15.24

    Why wouldn't you want to use a stack to manage print jobs?
    • Answer: Stacks use a “last-in, first-out” discipline. If you are the first one to submit a print job and lots of people add print jobs before the printer has a chance to deal with your job, they get their printouts first, and you have to wait until all other jobs are completed.

    Self Check 15.25

    In the sample code for a priority queue, we used a WorkOrder class. Could we have used strings instead?
    PriorityQueue<String> q = new PriorityQueue<String>();
    q.add("3 - Shampoo carpets");
    q.add("1 - Fix broken sink");
    q.add("2 - Order cleaning supplies");
    • Answer: Yes––the smallest string (in lexicographic ordering) is removed first. In the example, that is the string starting with 1, then the string starting with 2, and so on. However, the scheme breaks down if a priority value exceeds 9. For example, a string "10 - Line up braces" comes before "2 - Order cleaning supplies" in lexicographic order.

    Stack and Queue Applications

    • A stack can be used to check whether parentheses in an expression are balanced.
      When you see an opening parenthesis, push it on the stack.
      When you see a closing parenthesis, pop the stack.
      If the opening and closing parentheses don't match
         The parentheses are unbalanced. Exit.
      If at the end the stack is empty
         The parentheses are balanced.
         The parentheses are not balanced.
    • Walkthrough of the sample expression:
      Expression evaluation

    Stack and Queue Applications

    • Use a stack to evaluate expressions in reverse Polish notation.
      If you read a number
         Push it on the stack.
      Else if you read an operand
         Pop two values off the stack.
         Combine the values with the operand.
         Push the result back onto the stack.
      Else if there is no more input
         Pop and display the result.
    • Walkthrough of evaluating the expression 3 4 5 + ×:
      reverse polish evaluation


    Your browser does not support this feature

    Evaluating Algebraic Expressions with Two Stacks

    • Using two stacks, you can evaluate expressions in standard algebraic notation.
      • One stack for numbers, one for operatorsalgebraic expresion on a blackborad
    • Evaluating the top: 3 + 4
      stacks for 3 + 4

    Evaluating Algebraic Expressions with Two Stacks

    • Evaluate 3 x 4 + 5
      • Push until you get to the +
        evaluate expression
      • x (top of operator stack) has higher precedence than + , so evaluate the top
        evaluate the top

    Evaluating Algebraic Expressions with Two Stacks

    • Evaluate 3 + 4 × 5
      • Add x to the operator stack so we can get the next number
        algebraic expression
        alberaic expresion
      • Keep operators on the stack until they are ready to be evaluated
        algebraic expression

    Evaluating Algebraic Expressions with Two Stacks

    • Evaluating parentheses: 3 × (4 + 5)
      • Push ( on the stack
      • Keep pushing until we reach the )
      • Evaluate until we find the matching (
        Algebraic expression

    Evaluating Algebraic Expressions with Two Stacks

    • The algorithm
      If you read a number
         Push it on the number stack.
      Else if you read a (
         Push it on the operator stack.
      Else if you read an operator op
         While the top of the stack has a higher precedence than op
            Evaluate the top.
         Push op on the operator stack.
      Else if you read a )
         While the top of the stack is not a (
            Evaluate the top.
         Pop the (.
      Else if there is no more input
         While the operator stack is not empty
            Evaluate the top.
      At the end, the value on the number stack the the value of the expression

    Evaluating Algebraic Expressions with Two Stacks

    • Helper method to evaluate the top:
      Pop two numbers off the number stack.
      Pop an operator off the operator stack.
      Combine the numbers with that operator.
      Push the result on the number stack.


    • Use a stack to remember choices you haven't yet made so that you can backtrack to them.
    • Escaping a maze
      • You want to escape from a maze
      • You come to an intersection. What should you do?
      • Explore one of the paths
      • But remember the other paths.
      • If your chosen path doesn't work, you can
        • go back and try one of the other choices
    • Use a stack to remember the paths that still need to be tried.
    • The process of returning to a choice point and trying another choice is called backtracking.

    Backtracking - Maze Example

    • Start, at position (3, 4).
    • There are four possible paths. We push them all on a stack step one.
    • We pop off the topmost one, traveling north from (3, 4).
    • Following this path leads to position (1, 4).
      • We now push two choices on the stack, going west or east step 2 .
      • Both of them lead to dead ends step 3step 4.
    • Now we pop off the path from (3,4) going east.
      • That too is a dead end step 5.
    • Next is the path from (3, 4) going south.
    • Comes to an intersection at (5, 4).
      • Both choices are pushed on the stack step 6.
      • They both lead to dead ends step 7step 8.
    • Finally, the path from (3, 4) going west leads to an exit step 9.


    maze escape


    • Algorithm:
      Push all paths from the point on which you are standing on a stack.
      While the stack is not empty
         Pop a path from the stack.
         Follow the path until you reach an exit, intersection, or dead end.
         If you found an exit
      Else if you found an intersection
         Push all paths meeting at the intersection, except the current one, onto the stack.
    • This works if there are no cycles in the maze.
      • You never circle back to a previously visited intersection
    • You could use a queue instead of a stack.

    Self Check 15.26

    What is the value of the reverse Polish notation expression 2 3 4 + 5 × ×?
    • Answer: 70

    Self Check 15.27

    Why does the branch for the subtraction operator in the Calculator program not simply execute
    results.push(results.pop() - results.pop());
    • Answer: It would then subtract the first argument from the second. Consider the input 5 3 –. The stack contains 5 and 3, with the 3 on the top. Then results.pop() - results.pop() computes 3 – 5.

    Self Check 15.28

    In the evaluation of the expression 3 – 4 + 5 with the algorithm of Section 15.6.3, which operator gets evaluated first?
    • Answer: The – gets executed first because + doesn't have a higher precedence.

    Self Check 15.29

    In the algorithm of Section 15.6.3, are the operators on the operator stack always in increasing precedence?
    • Answer: No, because there may be parentheses on the stack. The parentheses separate groups of operators, each of which is in increasing precedence.

    Self Check 15.30

    Consider the following simple maze. Assuming that we start at the marked point and push paths in the order West, South, East, North, in which order are the lettered points visited, using the algorithm of Section 15.6.4?

    maze for selfcheck 30

    • Answer: A B E F G D C K J N