Ch15 The Java Collections Framework
Ch15 The Java Collections Framework
T h e J ava
Collections
Framework
Chapter Goals
Chapter Contents
Java for Everyone, 2e, Cay Horstmann, Copyright © 2013 John Wiley and Sons, Inc. All rights reserved.
W669
If you want to write a program that collects objects (such
as the stamps to the left), you have a number of choices. Of
course, you can use an array list, but computer scientists
have invented other mechanisms that may be better suited
for the task. In this chapter, we introduce the collection
classes and interfaces that the Java library offers. You will
learn how to use the Java collection classes, and how to
choose the most appropriate collection type for a problem.
‹‹interface›› ‹‹interface››
Collection Map
W670
15.1 An Overview of the Collections Framework W671
However, in many applications, you don’t really care about the order of the ele-
ments in a collection. Consider a mail-order dealer of books. Without customers
browsing the shelves, there is no need to order books by topic. Such a collection
without an intrinsic order is called a set—see Figure 3.
A set is an unordered
Because a set does not track the order of the elements, it can arrange them in a
collection of unique way that speeds up the operations of finding, adding, and removing elements. Com-
elements. puter scientists have invented mechanisms for this purpose. The Java library provides
classes that are based on two such mechanisms (called hash tables and binary search
trees). You will learn in this chapter how to choose between them.
Another way of gaining efficiency in a collection is to reduce the number of opera-
tions. A stack remembers the order of its elements, but it does not allow you to insert
elements in every position. You can add and remove elements only at the top—see
Figure 4.
In a queue, you add items to one end (the tail) and remove them from the other end
(the head). For example, you could keep a queue of books, adding required reading at
the tail and taking a book from the head whenever you have time to read another one.
We will discuss stacks and queues in Section 15.5.
A map keeps
Finally, a map manages associations between keys and values. Every key in the
associations map has an associated value. The map stores the keys, values, and the associations
between key and between them. For an example, consider a library that puts a bar code on each book.
value objects.
The program used to check books in and out needs to look up the book associated
with each bar code. A map associating bar codes with books can solve this problem—
see Figure 5. We will discuss maps in Section 15.4.
Values
O n l i n e E x a m p l e
A sample program
Table 1 The Methods of the Collection Interface
that demonstrates
several collection Collection<String> coll = The ArrayList class implements the Collection
classes. new ArrayList<String>(); interface.
Iterator<String> iter = coll.iterator() You use an iterator for visiting the elements in
the collection (see Section 15.2.3).
Self Check 1. A gradebook application stores a collection of quizzes. Should it use a list or
a set?
2. A student information system stores a collection of student records for a
university. Should it use a list or a set?
3. Why is a queue of books a better choice than a stack for organizing your
required reading?
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.
Practice It Now you can try these exercises at the end of the chapter: R15.1, R15.2, R15.3.
When you insert a new node into a linked list, only the neighboring node references
need to be updated (see Figure 7).
Figure 7
Inserting a Romeo
Node into a
Linked List
The same is true when you remove a node (see Figure 8). What’s the catch? Linked
lists allow speedy insertion and removal, but element access can be slow.
Figure 8
Removing a Tom Diana Harry
Node from a
Linked List
W674 Chapter 15 The Java Collections Framework
Note that the iterator class is also a generic type. A ListIterator<String> iterates
through a list of strings; a ListIterator<Book> visits the elements in a LinkedList<Book>.
Initially, the iterator points before the first element. You can move the iterator
A N I M AT I O N
position with the next method:
List Iterators iterator.next();
The next method throws a NoSuchElementException if you are already past the end of
the list. You should always call the iterator’s hasNext method before calling next—it
returns true if there is a next element.
if (iterator.hasNext())
{
iterator.next();
}
The next method returns the element that the iterator is passing. When you use a
ListIterator<String>, the return type of the next method is String. In general, the return
type of the next method matches the list iterator’s type parameter (which reflects the
type of the elements in the list).
You traverse all elements in a linked list of strings with the following loop:
while (iterator.hasNext())
{
String name = iterator.next();
Do something with name
}
As a shorthand, if your loop simply visits all elements of the linked list, you can use
the “for each” loop:
for (String name : employeeNames)
{
Do something with name
}
Then you don’t have to worry about iterators at all. Behind the scenes, the for loop
uses an iterator to visit all list elements.
next returns D
After calling next D H R T
After inserting J D J R
H T
R T
The nodes of the LinkedList class store two links: one to the next element and one
to the previous one. Such a list is called a doubly-linked list. You can use the previ-
ous and hasPrevious methods of the ListIter ator interface to move the iterator position
backward.
The add method adds an object after the iterator, then moves the iterator position
past the new element.
iterator.add("Juliet");
You can visualize insertion to be like typing text in a word processor. Each character
is inserted after the cursor, then the cursor moves past the inserted character (see Fig-
ure 9). Most people never pay much attention to this—you may want to try it out and
watch carefully how your word processor inserts characters.
The remove method removes the object that was returned by the last call to next or
previous. For example, this loop removes all names that fulfill a certain condition:
while (iterator.hasNext())
{
String name = iterator.next();
if (condition is fulfilled for name)
{
iterator.remove();
}
}
You have to be careful when calling remove. It can be called only once after calling
next or previous, and you cannot call it immediately after a call to add. If you call the
method improperly, it throws an IllegalStateException.
Table 3 summarizes the methods of the ListIterator interface. The ListIterator
interface extends a more general Iterator interface that is suitable for arbitrary col-
lections, not just lists. The table indicates which methods are specific to list iterators.
Following is a sample program that inserts strings into a list and then iterates
through the list, adding and removing elements. Finally, the entire list is printed. The
comments indicate the iterator position.
if (iter.hasPrevious()) hasPrevious returns true because the iterator is not at the beginning of
{ the list. previous and hasPrevious are ListIterator methods.
s = iter.previous();
}
iter.add("Diana"); Adds an element before the iterator position (ListIterator only). The
list is now [Diana, Juliet].
iter.next(); remove removes the last element returned by next or previous. The list is
iter.remove(); now [Diana].
15.2 Linked Lists W677
section_2/ListDemo.java
1 import java.util.LinkedList;
2 import java.util.ListIterator;
3
4 /**
5 This program demonstrates the LinkedList class.
6 */
7 public class ListDemo
8 {
9 public static void main(String[] args)
10 {
11 LinkedList<String> staff = new LinkedList<String>();
12 staff.addLast("Diana");
13 staff.addLast("Harry");
14 staff.addLast("Romeo");
15 staff.addLast("Tom");
16
17 // | in the comments indicates the iterator position
18
19 ListIterator<String> iterator = staff.listIterator(); // |DHRT
20 iterator.next(); // D|HRT
21 iterator.next(); // DH|RT
22
23 // Add more elements after second element
24
25 iterator.add("Juliet"); // DHJ|RT
26 iterator.add("Nina"); // DHJN|RT
27
28 iterator.next(); // DHJNR|T
29
30 // Remove last traversed element
31
32 iterator.remove(); // DHJN|T
33
34 // Print all elements
35
36 System.out.println(staff);
37 System.out.println("Expected: [Diana, Harry, Juliet, Nina, Tom]");
38 }
39 }
Program Run
[Diana, Harry, Juliet, Nina, Tom]
Expected: [Diana, Harry, Juliet, Nina, Tom]
Self Check 5. Do linked lists take more storage space than arrays of the same size?
6. Why don’t we need iterators with arrays?
7. Suppose the list lst 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();
iter.next();
iter.next();
iter.remove();
iter.next();
iter.add("E");
W678 Chapter 15 The Java Collections Framework
iter.next();
iter.add("F");
8. Write a loop that removes all strings with length less than four from a linked list
of strings called words.
9. Write a loop that prints every second element of a linked list of strings called
words.
Practice It Now you can try these exercises at the end of the chapter: R15.4, R15.7, P15.1.
15.3 Sets
As you learned in Section 15.1, a set organizes its values in an order that is optimized
for efficiency, which may not be the order in which you add elements. Inserting and
removing elements is faster with a set than with a list.
In the following sections, you will learn how to choose a set implementation and
how to work with sets.
After you construct the collection object, the implementation no longer matters;
only the interface is important.
Finally, to list all elements in the set, get an iterator. As with list iterators, you use the
next and hasNext methods to step through the set.
Iterator<String> iter = names.iterator();
while (iter.hasNext())
{
String name = iter.next();
Do something with name
}
15.3 Sets W681
You can also use the “for each” loop instead of explicitly using an iterator:
for (String name : names)
{
Do something with name
}
names = new HashSet<String>(); Use a TreeSet if you need to visit the elements
in sorted order.
names.add("Romeo"); Now names.size() is 1.
The following program shows a practical application of sets. It reads in all words
from a dictionary file that contains correctly spelled words and places them in a set.
It then reads all words from a document—here, the book Alice in Wonderland—into
a second set. Finally, it prints all words from that set that are not in the dictionary
set. These are the potential misspellings. (As you can see from the output, we used an
American dictionary, and words with British spelling, such as clamour, are flagged as
potential errors.)
section_3/SpellCheck.java
1 import java.util.HashSet;
2 import java.util.Scanner;
3 import java.util.Set;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6
7 /**
8 This program checks which words in a file are not present in a dictionary.
9 */
10 public class SpellCheck
11 {
12 public static void main(String[] args)
13 throws FileNotFoundException
14 {
15 // Read the dictionary and the document
16
17 Set<String> dictionaryWords = readWords("words");
18 Set<String> documentWords = readWords("alice30.txt");
19
20 // Print all words that are in the document but not the dictionary
21
22 for (String word : documentWords)
23 {
24 if (!dictionaryWords.contains(word))
25 {
26 System.out.println(word);
27 }
28 }
29 }
30
31 /**
32 Reads all words from a file.
33 @param filename the name of the file
34 @return a set with all lowercased words in the file. Here, a
35 word is a sequence of upper- and lowercase letters.
36 */
37 public static Set<String> readWords(String filename)
38 throws FileNotFoundException
39 {
40 Set<String> words = new HashSet<String>();
41 Scanner in = new Scanner(new File(filename));
42 // Use any characters other than a-z or A-Z as delimiters
43 in.useDelimiter("[^a-zA-Z]+");
44 while (in.hasNext())
45 {
46 words.add(in.next().toLowerCase());
47 }
15.3 Sets W683
48 return words;
49 }
50 }
Program Run
neighbouring
croqueted
pennyworth
dutchess
comfits
xii
dinn
clamour
...
Self Check 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?
11. Why are set iterators different from list iterators?
12. What is wrong with the following test to check whether the Set<String> s con-
tains the elements "Tom", "Diana", and "Harry"?
if (s.toString().equals("[Tom, Diana, Harry]")) . . .
13. How can you correctly implement the test of Self Check 12?
14. Write a loop that prints all elements that are in both Set<String> s and
Set<String> t.
15. Suppose you changed line 40 of the SpellCheck program to use a TreeSet instead of
a HashSet. How would the output change?
Practice It Now you can try these exercises at the end of the chapter: P15.7, P15.8, P15.13.
15.4 Maps
The HashMap and
A map allows you to associate elements from a key set with elements from a value
TreeMap classes collection. You use a map when you want to look up objects by using a key. For exam-
both implement ple, Figure 10 shows a map from the names of people to their favorite colors.
the Map interface.
Just as there are two kinds of set implementations, the Java library has two imple-
mentations for the Map interface: HashMap and TreeMap.
After constructing a HashMap or TreeMap, you can 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, simply by calling put again:
favoriteColors.put("Juliet", Color.BLUE);
If you ask for a key that isn’t associated with any values, then the get method returns
null.
To remove an association, call the remove method with the key:
favoriteColors.remove("Juliet");
Keys Values
Romeo
Adam
Eve
Juliet
Figure 10 A Map
section_4/MapDemo.java
1 import java.awt.Color;
2 import java.util.HashMap;
3 import java.util.Map;
4 import java.util.Set;
5
6 /**
7 This program demonstrates a map that maps names to colors.
8 */
9 public class MapDemo
10 {
11 public static void main(String[] args)
12 {
13 Map<String, Color> favoriteColors = new HashMap<String, Color>();
14 favoriteColors.put("Juliet", Color.BLUE);
15 favoriteColors.put("Romeo", Color.GREEN);
16 favoriteColors.put("Adam", Color.RED);
17 favoriteColors.put("Eve", Color.BLUE);
18
19 // Print all keys and values in the map
20
21 Set<String> keySet = favoriteColors.keySet();
W686 Chapter 15 The Java Collections Framework
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 16. What is the difference between a set and a map?
17. Why is the collection of the keys of a map a set and not a list?
18. Why is the collection of the values of a map not a set?
19. Suppose you want to track how many times each word occurs in a document.
Declare a suitable map variable.
20. What is a Map<String, HashSet<String>>? Give a possible use for such a structure.
Practice It Now you can try these exercises at the end of the chapter: R15.17, P15.9, P15.14.
Similarly, for a map, determine the types of the keys and the associated values. If you want
to look up books by ID, you can use a Map<Integer, Book> or Map<String, Book>, depending on
your ID type.
Step 3 Determine whether element or key order matters.
When you visit elements from a collection or keys from a map, do you care about the order in
which they are visited? You have several choices:
• Elements or keys must be sorted. Use a TreeSet or TreeMap. Go to Step 6.
• Elements must be in the same order in which they were inserted. Your choice is now
narrowed down to a LinkedList or an ArrayList.
• It doesn’t matter. As long as you get to visit all elements, you don’t care in which order. If
you chose a map in Step 1, use a HashMap and go to Step 5.
Step 5 For hash sets and maps, decide whether you need to implement the hashCode and equals
methods.
• If your elements or keys belong to a class that someone else implemented, check whether
the class has its own hashCode and equals methods. If so, you are all set. This is the case for
most classes in the standard Java library, such as String, Integer, Rectangle, and so on.
• If not, decide whether you can compare the elements by identity. This is the case if you
never construct two distinct elements with the same contents. In that case, you need not
do anything—the hashCode and equals methods of the Object class are appropriate.
• Otherwise, you need to implement your own equals and hashCode methods––see Special
Topics 9.7 and Special Topic 15.1.
"eat" 100184
"tea" 114704
"Juliet" –2065036585
"Ugh" 84982
"VII" 84982
15.4 Maps W689
15.5.1 Stacks
A stack is a collection
A stack lets you insert and remove elements only
of elements with at one end, traditionally called the top of the stack.
“last-in, first-out” New items can be added to the top of the stack.
retrieval.
Items are removed from the top of the stack as well.
Therefore, they are removed in the order that is
opposite from the order in which they have been
added, called last-in, first-out or LIFO order. For
example, if you add items A, B, and C and then remove
them, you obtain C, B, and A. With stacks, the addi-
tion and removal operations are called push and pop.
Stack<String> s = new Stack<String>(); The last pancake that has been
s.push("A"); s.push("B"); s.push("C"); added to this stack will be the
while (s.size() > 0) first one that is consumed.
{
System.out.print(s.pop() + " "); // Prints C B A
}
There are many applications for stacks in computer science. Consider the undo fea-
ture of a word processor. It keeps the issued commands in a stack. When you select
“Undo”, the last command is undone, then the next-to-last, and so on.
Another important example is the run-time stack that a processor or virtual
machine keeps to store the values of variables in nested methods. Whenever 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.
The Undo key pops You will see other applications in Section 15.6.
commands off a The Java library provides a simple Stack class with methods push, pop, and peek—the
stack, so that the last latter gets the top element of the stack but does not remove it (see Table 7).
command is the first
to be undone.
15.5.2 Queues
A queue is a
A queue lets you add items to one end of
collection of the queue (the tail) and remove them from
elements with the other end of the queue (the head).
“first-in, first-out”
retrieval.
Queues yield items in a first-in, first-out
or FIFO fashion. Items are removed in
the same order in which they were added.
A typical application is a print queue.
A printer may be accessed by several
applications, perhaps running on differ-
ent computers. If each of the applications
tried to access the printer at the same time, To visualize a queue, think of people lining up.
the printout would be garbled. Instead,
each application places its print data into a file and adds that file to the print queue.
When the printer is done printing one file, it retrieves the next one from the queue.
Therefore, print jobs are printed using the “first-in, first-out” rule, which is a fair
arrangement for users of the shared printer.
The Queue interface in the standard Java library has methods add to add an element
to the tail of the queue, remove to remove the head of the queue, and peek to get the
head element of the queue without removing it (see Table 8).
The LinkedList class implements the Queue interface. Whenever you need a queue,
simply 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
The standard library provides several queue classes that we do not discuss in this
book. Those classes are intended for work sharing when multiple activities (called
threads) run in parallel.
int head = q.remove(); Removes the head of the queue; head is set to 1 and q is [2, 3].
head = q.peek(); Gets the head of the queue without removing it; head is set to 2.
When removing an
A priority queue collects elements, each of which has a priority. A typical example
element from a of a priority queue is a collection of work requests, some of which may be more
priority queue, the urgent than others. Unlike a regular queue, the priority queue does not maintain a
element with the
most urgent priority
first-in, first-out discipline. Instead, elements are retrieved according to their prior-
is retrieved. ity. In other words, new items can be inserted in any order. But whenever an item is
removed, it is the item with the most urgent priority.
W692 Chapter 15 The Java Collections Framework
When calling q.remove() for the first time, the work order with priority 1 is
removed. The next call to q.remove() removes the work order whose priority
When you retrieve an item from is highest among those remaining in the queue—in our example, the work
a priority queue, you always
get the most urgent one.
order with priority 2. If there happen to be two elements with the same pri-
ority, the priority queue will break ties arbitrarily.
Because the priority queue needs to be able to tell which element is the smallest,
the added elements should belong to a class that implements the Comparable interface.
(See Section 9.6.3 for a description of that interface type.)
Table 9 shows the methods of the PriorityQueue class in the standard Java library.
O NLINE E x a m p l e
Table 9 Working with Priority Queues
Programs that
demonstrate stacks,
queues, and priority
PriorityQueue<Integer> q = This priority queue holds Integer objects. In
queues. new PriorityQueue<Integer>(); practice, you would use objects that describe tasks.
q.add(3); q.add(1); q.add(2); Adds values to the priority queue.
int first = q.remove(); Each call to remove removes the lowest priority item:
int second = q.remove(); first is set to 1, second to 2.
int next = q.peek(); Gets the smallest value in the priority queue without
removing it.
Practice It Now you can try these exercises at the end of the chapter: R15.12, P15.3, P15.4.
Increment a counter when you see a ( and decrement it when you see a ). The counter
should never be negative, and it should be zero at the end of the expression.
That works for expressions in Java, but in mathematical notation, one can have
more than one kind of parentheses, such as
–{ [b ⋅ b - (4 ⋅ a ⋅ c ) ] / (2 ⋅ a) }
To see whether such an expression is correctly formed, place the parentheses on a
stack:
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
O NLINE E x a m p l e The parentheses are balanced.
A program for Else
checking balanced The parentheses are not balanced.
parentheses.
Use a stack to
Consider how you write arithmetic expressions, such as (3 + 4) × 5. The parentheses
evaluate expressions are needed so that 3 and 4 are added before multiplying the result by 5.
in reverse Polish However, you can eliminate the parentheses if you write the operators after the
notation.
numbers, like this: 3 4 + 5 × (see Random Fact 15.2 on page W701). To evaluate this expres-
sion, apply + to 3 and 4, yielding 7, and then simplify 7 5 × to 35. It gets trickier for
complex expressions. For example, 3 4 5 + × means to compute 4 5 + (that is, 9), and
then evaluate 3 9 ×. If we evaluate this expression left-to-right, we need to leave the 3
somewhere while we work on 4 5 +. Where? We put it on a stack. The algorithm for
evaluating reverse Polish expressions is simple:
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.
section_6_2/Calculator.java
1 import java.util.Scanner;
2 import java.util.Stack;
3
4 /**
5 This calculator uses the reverse Polish notation.
6 */
7 public class Calculator
8 {
9 public static void main(String[] args)
10 {
11 Scanner in = new Scanner(System.in);
12 Stack<Integer> results = new Stack<Integer>();
13 System.out.println("Enter one number or operator per line, Q to quit. ");
14 boolean done = false;
15.6 Stack and Queue Applications W695
15 while (!done)
16 {
17 String input = in.nextLine();
18
19 // If the command is an operator, pop the arguments and push the result
20
21 if (input.equals("+"))
22 {
23 results.push(results.pop() + results.pop());
24 }
25 else if (input.equals("-"))
26 {
27 Integer arg2 = results.pop();
28 results.push(results.pop() - arg2);
29 }
30 else if (input.equals("*") || input.equals("x"))
31 {
32 results.push(results.pop() * results.pop());
33 }
34 else if (input.equals("/"))
35 {
36 Integer arg2 = results.pop();
37 results.push(results.pop() / arg2);
38 }
39 else if (input.equals("Q") || input.equals("q"))
40 {
41 done = true;
42 }
43 else
44 {
45 // Not an operator--push the input value
46
47 results.push(Integer.parseInt(input));
48 }
49 System.out.println(results);
50 }
51 }
52 }
First, consider a simple example, the expression 3 + 4. We push the numbers on the
number stack and the operators on the operator stack. Then we pop both numbers
and the operator, combine the numbers with the operator, and push the result.
1 3 +4
2 3 + 4
4 7 The result is 7.
1 3 ×4+5
2 3 × 4+5
3 4 +5 Evaluate × before +.
3 ×
Because × has a higher precedence than +, we are ready to evaluate the top:
4 12 + 5
With the expression, 3 + 4 × 5, we add × to the operator stack because we must first
read the next number; then we can evaluate × and then the +:
1 3 +4×5
2 3 + 4+5
15.6 Stack and Queue Applications W697
4 4 × 5
3 +
In other words, we keep operators on the stack until they are ready to be evaluated.
Here is the remainder of the computation:
To see how parentheses are handled, consider the expression 3 × (4 + 5). A ( is pushed
on the operator stack. The + is pushed as well. When we encounter the ), we know
that we are ready to evaluate the top until the matching ( reappears:
1 3 × (4 + 5)
2 3 × (4 + 5)
4 4 ( + 5)
3 ×
5 + 5)
4 (
3 ×
15.6.4 Backtracking
Use a stack to
Suppose you are inside a maze. You need to find the exit.
remember choices What should you do when you come to an intersection?
you haven’t yet made You can continue exploring one of the paths, but you
so that you can
backtrack to them.
will want to remember the other ones. If your chosen
path didn’t work, you can go back to one of the other
choices and try again.
Of course, as you go along one path, you may reach
further intersections, and you need to remember your A stack can be used to track
choice again. Simply use a stack to remember the paths positions in a maze.
that still need to be tried. The process of returning to a
choice point and trying another choice is called backtracking. By using a stack, you
return to your more recent choices before you explore the earlier ones.
Figure 11 shows an example. We start at a point in the maze, at position (3, 4).
There are four possible paths. We push them all on a stack 1 . 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 2 . Both of them lead to dead ends
3 4.
Now we pop off the path from (3,4) going east. That too is a dead end 5 . Next is
the path from (3, 4) going south. At (5, 4), it comes to an intersection. Both choices
are pushed on the stack 6 . They both lead to dead ends 7 8 .
Finally, the path from (3, 4) going west leads to an exit 9 .
15.6 Stack and Queue Applications W699
1 0 1 2 3 4 5 6 7 6 0 1 2 3 4 5 6 7
0 34↑ 0 54↓
1 1
2 34→ 2 54←
3 3
4
34↓ 4
34←
5 34← 5
6 6
7 7
2 0 1 2 3 4 5 6 7 7 0 1 2 3 4 5 6 7
0 14→ 0 54←
1 1
2 14← 2 34←
3 3
4
34→ 4
5 34↓ 5
6 6
7 34← 7
3 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7
0 14← 0 34←
1 1
2 34→ 2
3 3
4
34↓ 4
5 34← 5
6 6
7 7
4 0 1 2 3 4 5 6 7 9 0 1 2 3 4 5 6 7
0 34→ 0
1 1
2 34↓ 2
3 3
4
34← 4
5 5
6 6
7 7
5 0 1 2 3 4 5 6 7
0 34↓
1
2 34←
3
4
5
6
7
Using a stack, we have found a path out of the maze. Here is the pseudocode for
our maze-finding 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
Congratulations!
Else if you found an intersection
Push all paths meeting at the intersection, except the current one, onto the stack.
This algorithm will find an exit from the maze, provided that the maze has no cycles.
If it is possible that you can make a circle and return to a previously visited intersec-
tion along a different sequence of paths, then you need to work harder––see Exercise
P15.25.
W700 Chapter 15 The Java Collections Framework
How you implement this algorithm depends on the description of the maze. In
the example code, we use a two-dimensional array of characters, with spaces for cor-
ridors and asterisks for walls, like this:
********
* *
**** ***
*
**** ***
* ***
**** ***
********
In the example code, a Path object is constructed with a starting position and a direc-
tion (North, East, South, or West). The Maze class has a method that extends a path
until it reaches an intersection or exit, or until it is blocked by a wall, and a method
that computes all paths from an intersection point.
O n l i n e E x a m p l e Note that you can use a queue instead of a stack in this algorithm. Then you
A complete program explore the earlier alternatives before the later ones. This can work just as well for
demonstrating finding an answer, but it isn’t very intuitive in the context of exploring a maze—you
backtracking.
would have to imagine being teleported back to the initial intersections rather than
just walking back to the last one.
Self Check 26. What is the value of the reverse Polish notation expression 2 3 4 + 5 × ×?
27. Why does the branch for the subtraction operator in the Calculator program not
simply execute
results.push(results.pop() - results.pop());
28. In the evaluation of the expression 3 – 4 + 5 with the algorithm of Section 15.6.3,
which operator gets evaluated first?
29. In the algorithm of Section 15.6.3, are the operators on the operator stack always
in increasing precedence?
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 let-
tered points visited, using the algorithm of Section 15.6.4?
A B C D
E F G
H I J K
L M
N
Practice It Now you can try these exercises at the end of the chapter: R15.21, P15.21, P15.22,
P15.25, P15.26.
C h a p t e r Su m m a r y
• A linked list consists of a number of nodes, each of which has a reference to the
next node.
• Adding and removing elements at a given position in a linked list is efficient.
• Visiting the elements of a linked list in sequential order is efficient, but random
access is not.
• You use a list iterator to access elements inside a linked list.
• The HashSet and TreeSet classes both implement the Set interface.
• Set implementations arrange the elements so that they can locate them quickly.
• You can form hash sets holding objects of type String,
Integer, Double, Point, Rectangle, or Color.
• You can form tree sets for any class that implements the
Comparable interface, such as String or Integer.
• Sets don’t have duplicates. Adding a duplicate of an element
that is already present is ignored.
• A set iterator visits the elements in the order in which the set
implementation keeps them.
• You cannot add an element to a set at an iterator position.
ISBN 978-0-470-10555-9
90000
9
ISBN 978-0-470-10554-2
780470 105542
90000
ISBN 978-0-471-79191-1
90000
9
ISBN 978-0-470-50948-1
780470 509481
90000
ISBN 978-0-470-38329-2
90000
• The HashMap and TreeMap classes both implement the Map interface.
• To find all keys and values in a map, iterate through the key set and find the values
9 780470 105559 9 780471 791911 9 780470 383292
Use the Java classes for stacks, queues, and priority queues.
S ta n d a r d L i b r a r y I t e m s I n t r o duc e d i n t h i s C h a p t e r
Review Exercises
•• R15.1 An invoice contains a collection of purchased items. Should that collection be imple-
mented as a list or set? Explain your answer.
•• R15.2 Consider a program that manages an appointment calendar. Should it place the
appointments into a list, stack, queue, or priority queue? Explain your answer.
••• R15.3 One way of implementing a calendar is as a map from date objects to event objects.
However, that only works if there is a single event for a given date. How can you use
another collection type to allow for multiple events on a given date?
• R15.4 Explain what the following code prints. Draw a picture of the linked list after each
step.
LinkedList<String> staff = new LinkedList<String>();
staff.addFirst("Harry");
staff.addFirst("Diana");
staff.addFirst("Tom");
System.out.println(staff.removeFirst());
System.out.println(staff.removeFirst());
System.out.println(staff.removeFirst());
• R15.5 Explain what the following code prints. Draw a picture of the linked list after each
step.
LinkedList<String> staff = new LinkedList<String>();
staff.addFirst("Harry");
staff.addFirst("Diana");
staff.addFirst("Tom");
System.out.println(staff.removeLast());
System.out.println(staff.removeFirst());
System.out.println(staff.removeLast());
W704 Chapter 15 The Java Collections Framework
• R15.6 Explain what the following code prints. Draw a picture of the linked list after each
step.
LinkedList<String> staff = new LinkedList<String>();
staff.addFirst("Harry");
staff.addLast("Diana");
staff.addFirst("Tom");
System.out.println(staff.removeLast());
System.out.println(staff.removeFirst());
System.out.println(staff.removeLast());
• R15.7 Explain what the following code prints. Draw a picture of the linked list and the
iterator position after each step.
LinkedList<String> staff = new LinkedList<String>();
ListIterator<String> iterator = staff.listIterator();
iterator.add("Tom");
iterator.add("Diana");
iterator.add("Harry");
iterator = staff.listIterator();
if (iterator.next().equals("Tom")) { iterator.remove(); }
while (iterator.hasNext()) { System.out.println(iterator.next()); }
• R15.8 Explain what the following code prints. Draw a picture of the linked list and the
iterator position after each step.
LinkedList<String> staff = new LinkedList<String>();
ListIterator<String> iterator = staff.listIterator();
iterator.add("Tom");
iterator.add("Diana");
iterator.add("Harry");
iterator = staff.listIterator();
iterator.next();
iterator.next();
iterator.add("Romeo");
iterator.next();
iterator.add("Juliet");
iterator = staff.listIterator();
iterator.next();
iterator.remove();
while (iterator.hasNext()) { System.out.println(iterator.next()); }
•• R15.9 What advantages do linked lists have over arrays? What disadvantages do they have?
•• R15.10 Suppose you need to organize a collection of telephone numbers for a company
division. There are currently about 6,000 employees, and you know that the phone
switch can handle at most 10,000 phone numbers. You expect several hundred look
ups against the collection every day. Would you use an array list or a linked list to
store the information?
•• R15.11 Suppose you need to keep a collection of appointments. Would you use a linked list
or an array list of Appointment objects?
• R15.12 Suppose you write a program that models a card deck. Cards are taken from the
top of the deck and given out to players. As cards are returned to the deck, they are
placed on the bottom of the deck. Would you store the cards in a stack or a queue?
• R15.13 Suppose the strings "A" . . . "Z" are pushed onto a stack. Then they are popped off the
stack and pushed onto a second stack. Finally, they are all popped off the second
stack and printed. In which order are the strings printed?
Programming Exercises W705
•• R15.15 The union of two sets A and B is the set of all elements that are contained in A, B, or
both. The intersection is the set of all elements that are contained in A and B. How
can you compute the union and intersection of two sets, using the add and contains
methods, together with an iterator?
•• R15.16 How can you compute the union and intersection of two sets, using some of the
methods that the java.util.Set interface provides, but without using an iterator?
(Look up the interface in the API documentation.)
• R15.17 Can a map have two keys with the same value? Two values with the same key?
••• R15.20 Verify that the strings "VII" and "Ugh" have the same hash code.
• R15.21 Consider the algorithm for traversing a maze from Section 15.6.4 Assume that we
start at position A and push in the order West, South, East, and North. In which
order will the lettered locations of the sample maze be visited?
O P Q R
L M N
J K
G H I
F
A B C D
Programming Exercises
• P15.4 Your task is to break a number into its individual digits, for example, to turn 1729
into 1, 7, 2, and 9. It is easy to get the last digit of a number n as n % 10. But that gets
the numbers in reverse order. Solve this problem with a stack. Your program should
ask the user for an integer, then print its digits separated by spaces.
•• P15.5 A homeowner rents out parking spaces in a driveway during special events. The
driveway is a “last-in, first-out” stack. Of course, when a car owner retrieves a
vehicle that wasn’t the last one in, the cars blocking it must temporarily move to
the street so that the requested vehicle can leave. Write a program that models this
behavior, using one stack for the driveway and one stack for the street. Use integers
as license plate numbers. Positive numbers add a car, negative numbers remove a car,
zero stops the simulation. Print out the stack after each operation is complete.
• P15.6 Implement a to do list. Tasks have a priority between 1 and 9, and a description.
When the user enters the command add priority description, the program adds a new
task. When the user enters next, the program removes and prints the most urgent
task. The quit command quits the program. Use a priority queue in your solution.
• P15.7 Write a program that reads text from a file and breaks it up into individual words.
Insert the words into a tree set. At the end of the input file, print all words, followed
by the size of the resulting set. This program determines how many unique words a
text file has.
•• P15.8 Implement the sieve of Eratosthenes: a method for computing
prime numbers, known to the ancient Greeks. This method
will compute all prime numbers up to n. Choose an n.
First insert all numbers from 2 to n into a set. Then erase all
multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, . . . . Erase
all multiples of 3; that is, 6, 9, 12, 15, . . . . Go up to n . Then
print the set.
•• P15.9 Write a program that keeps a map in which both keys and
values are strings—the names of students and their course
grades. Prompt the user of the program to add or remove students, to modify grades,
or to print all grades. The printout should be sorted by name and formatted like this:
Carl: B+
Joe: C
Sarah: A
••• P15.10 Reimplement Exercise P15.9 so that the keys of the map are objects of class Student.
A student should have a first name, a last name, and a unique integer ID. For grade
changes and removals, lookup should be by ID. The printout should be sorted
by last name. If two students have the same last name, then use the first name as a
tie breaker. If the first names are also identical, then use the integer ID. Hint: Use
two maps.
••• P15.11 Write a class Polynomial that stores a polynomial such as
p( x) = 5 x10 + 9 x7 − x − 10
as a linked list of terms. A term contains the coefficient and the power of x. For
example, you would store p(x) as
Supply methods to add, multiply, and print polynomials. Supply a constructor that
makes a polynomial from a single term. For example, the polynomial p can be
constructed as
Polynomial p = new Polynomial(new Term(-10, 0));
p.add(new Polynomial(new Term(-1, 1)));
p.add(new Polynomial(new Term(9, 7)));
p.add(new Polynomial(new Term(5, 10)));
Then compute p( x) × p( x) .
Polynomial q = p.multiply(p);
q.print();
••• P15.12 Repeat Exercise P15.11, but use a Map<Integer, Double> for the coefficients.
• P15.13 Insert all words from a large file (such as the novel “War and Peace”, which is avail
able on the Internet) into a hash set and a tree set. Time the results. Which data
structure is faster?
••• P15.14 Write a program that reads a Java source file and produces an index of all identifiers
in the file. For each identifier, print all lines in which it occurs. For simplicity, we
will consider each string consisting only of letters, numbers, and underscores
an identifer. Declare a Scanner in for reading from the source file and call
in.useDelimiter("[^A-Za-z0-9_]+"). Then each call to next returns an identifier.
•• P15.15 Try to find two words with the same hash code in a large file. Keep a Map<Integer,
HashSet<String>>. When you read in a word, compute its hash code h and put the
word in the set whose key is h. Then iterate through all keys and print the sets whose
size is > 1.
•• P15.16 Supply compatible hashCode and equals methods to the Student class described in
Exercise P15.10. Test the hash code by adding Student objects to a hash set.
• P15.17 Supply compatible hashCode and equals methods to the BankAccount class of Chapter 8.
Test the hashCode method by printing out hash codes and by adding BankAccount
objects to a hash set.
•• P15.18 A labeled point has x- and y-coordinates and a string label. Provide a class Labeled
Point with a constructor LabeledPoint(int x, int y, String label) and hashCode and
equals methods. Two labeled points are considered the same when they have the
same location and label.
•• P15.19 Reimplement the LabeledPoint class of the preceding exercise by storing the location
in a java.awt.Point object. Your hashCode and equals methods should call the hashCode
and equals methods of the Point class.
•• P15.20 Modify the LabeledPoint class of Exercise P15.18 so that it implements the Compa-
rable interface. Sort points first by their x-coordinates. If two points have the same
x-coordinate, sort them by their y-coordinates. If two points have the same x- and
y-coordinates, sort them by their label. Write a tester program that checks all cases
by inserting points into a TreeSet.
• P15.21 Write a program that checks whether a sequence of HTML tags is properly nested.
For each opening tag, such as <p>, there must be a closing tag </p>. A tag such as <p>
may have other tags inside, for example
<p> <ul> <li> </li> </ul> <a> </a> </p>
W708 Chapter 15 The Java Collections Framework
The inner tags must be closed before the outer ones. Your program should process a
file containing tags. For simplicity, assume that the tags are separated by spaces, and
that there is no text inside the tags.
• P15.22 Add a % (remainder) operator to the expression calculator of Section 15.6.3.
•• P15.23 Add a ^ (power) operator to the expression calculator of Section 15.6.3. For example,
2 ^ 3 evaluates to 8. As in mathematics, your power operator should be evaluated
from the right. That is, 2 ^ 3 ^ 2 is 2 ^ (3 ^ 2), not (2 ^ 3) ^ 2. (That’s more useful
because you could get the latter as 2 ^ (3 × 2).)
••• P15.24 Modify the expression calculator of Section 15.6.3 to convert an expression into
reverse Polish notation. Hint: Instead of evaluating the top and pushing the result,
append the instructions to a string.
••• P15.25 Modify the maze solver program of Section 15.6.4 to handle mazes with cycles. Keep
a set of visited intersections. When you have previously seen an intersection, treat it
as a dead end and do not add paths to the stack.
••• P15.26 In a paint program, a “flood fill” fills all empty pixels of a drawing with a given color,
stopping when it reaches occupied pixels. In this exercise, you will implement a
simple variation of this algorithm, flood-filling a 10 × 10 array of integers that are
initially 0.
Prompt for the starting row and column.
Push the (row, column) pair onto a stack.
You will need to provide a simple Pair class.
Repeat the following operations until the stack is empty.
Pop off the (row, column) pair from the top of the stack.
If it has not yet been filled, fill the corresponding array location with a number 1, 2, 3, and so on
(to show the order in which the square is filled).
Push the coordinates of any unfilled neighbors in the north, east, south, or west direction on the stack.
When you are done, print the entire array.
• P15.27 Repeat Exercise P15.26, but use a queue instead.
•• P15.28 Use a stack to enumerate all permutations of a string. Suppose you want to find all
permutations of the string meat.
Push the string +meat on the stack.
While the stack is not empty
Pop off the top of the stack.
If that string ends in a + (such as tame+)
Remove the + and add the string to the list of permutations.
Else
Remove each letter in turn from the right of the +.
Insert it just before the +.
Push the resulting string on the stack.
For example, after popping e+mta, you push em+ta, et+ma, and ea+mt.
•• Business P15.30 An airport has only one runway. When it is busy, planes wishing to take off or land
have to wait. Implement a simulation, using two queues, one each for the planes
waiting to take off and land. Landing planes get priority. The user enters commands
takeoff flightSymbol, land flightSymbol, next, and quit. The first two commands place
the flight in the appropriate queue. The next command finishes the current takeoff or
landing and enables the next one, printing the action (takeoff or land) and the flight
symbol.
•• Business P15.31 Suppose you buy 100 shares of a stock at $12 per share, then another 100 at $10 per
share, and then sell 150 shares at $15. You have to pay taxes on the gain, but exactly
what is the gain? In the United States, the FIFO rule holds: You first sell all shares
of the first batch for a profit of $300, then 50 of the shares from the second batch, for
a profit of $250, yielding a total profit of $550. Write a program that can make these
calculations for arbitrary purchases and sales of shares in a single company. The
user enters commands buy quantity price, sell quantity (which causes the gain to be
displayed), and quit. Hint: Keep a queue of objects of a class Block that contains the
quantity and price of a block of shares.
••• Business P15.32 Extend Exercise P15.31 to a program that can handle shares of multiple compa-
nies. The user enters commands buy symbol quantity price and sell symbol quantity.
Hint: Keep a Map<String, Queue<Block>> that manages a separate queue for each stock
symbol.
••• Business P15.33 Consider the problem of finding the least expensive routes to all cities in a network
from a given starting point.
Pendleton 2
Pierre
8
3
4 3 Peoria 5 2
Pueblo Princeton
4 Pittsburgh
3
10 4
5
Phoenix
5
Pensacola
For example, in this network, the least expensive route from Pendleton to Peoria has
cost 8 (going through Pierre and Pueblo).
The following helper class expresses the distance to another city:
public class DistanceTo implements Comparable<DistanceTo>
{
private String target;
private int distance;
W710 Chapter 15 The Java Collections Framework
A n s w e r s t o S e l f- C h e c k Q u e s t i o n s
10. Adding and removing elements as well as test- 21. This way, we can ensure that only queue
ing for membership is faster with sets. operations can be invoked on the q object.
11. Sets do not have an ordering, so it doesn’t 22. Depending on whether you consider the 0
make sense to add an element at a particular position the head or the tail of the queue, you
iterator position, or to traverse a set backward. would either add or remove elements at that
12. You do not know in which order the set keeps position. Both are expensive operations.
the elements. 23. A B C
13. Here is one possibility: 24. Stacks use a “last-in, first-out” discipline. If
if (s.size() == 3 && s.contains("Tom") you are the first one to submit a print job and
&& s.contains("Diana") lots of people add print jobs before the printer
&& s.contains("Harry")) has a chance to deal with your job, they get
. . . their printouts first, and you have to wait until
14. for (String str : s) all other jobs are completed.
{
if (t.contains(str))
25. Yes––the smallest string (in lexicographic
{ ordering) is removed first. In the example,
System.out.println(str); 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
15. The words would be listed in sorted order. 9. For example, a string "10 - Line up braces"
16. A set stores elements. A map stores associa- comes before "2 - Order cleaning supplies" in
tions between keys and values. lexicographic order.
17. The ordering does not matter, and you cannot 26. 70.
have duplicates. 27. It would then subtract the first argument from
18. Because it might have duplicates. the second. Consider the input 5 3 –. The stack
contains 5 and 3, with the 3 on the top. Then
19. Map<String, Integer> wordFrequency;
results.pop() - results.pop() computes 3 – 5.
Note that you cannot use a Map<String, int>
28. The – gets executed first because + doesn’t
because you cannot use primitive types as type
have a higher precedence.
parameters in Java.
29. No, because there may be parentheses on
20. It associates strings with sets of strings. One
the stack. The parentheses separate groups
application would be a thesaurus that lists
of operators, each of which is in increasing
synonyms for a given word. For example, the
precedence.
key "improve" might have as its value the set
["ameliorate", "better", "enhance", "enrich", 30. ABEFGDCKJN
"perfect", "refine"].