Program 19.2 (Run)
Old Accounts: [fbrue, gharris, lhung, tmiller]
Current Accounts: [ascott, fbrue, wtubbs]
Process Accounts: [ascott, fbrue, gharris, lhung, tmiller, wtubbs]
New Accounts: [ascott, wtubbs]
Carryover Accounts: [fbrue]
Obsolete Accounts: [gharris, lhung, tmiller]
Ordered Set Operations Ordered Set Operations
If a set is ordered, set operations can be If a set is ordered, set operations can be performed by using iterators that scan performed by using iterators that scan
each set.
each set.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Ordered Set Intersection Ordered Set Intersection
Algorithm Algorithm
The ordered set-intersection algorithm The ordered set-intersection algorithm
uses iterators to make a pairwise scan of uses iterators to make a pairwise scan of
the elements in the two sets. At each the elements in the two sets. At each
step, a comparison is made between step, a comparison is made between
elements and if a match occurs, the value elements and if a match occurs, the value
belongs to the intersection.
belongs to the intersection.
Ordered Set Intersection Ordered Set Intersection
Algorithm (continued)
Algorithm (continued)
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
advance() advance()
// if more elements remain, return the // next value; otherwise, return null
private static <T> T advance(Iterator<T> iter) {
T value = null;
if (iter.hasNext())
value = iter.next();
return value;
}
orderedIntersection() orderedIntersection()
public static <T extends Comparable<? super T>>
TreeSet<T> orderedIntersection(TreeSet<T> lhs, TreeSet<T> rhs)
{
// construct intersection
TreeSet<T> setIntersection = new TreeSet<T>();
// iterators that traverse the sets Iterator<T> lhsIter = lhs.iterator(), rhsIter = rhs.iterator();
T lhsValue, rhsValue;
lhsValue = advance(lhsIter);
rhsValue = advance(rhsIter);
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
orderedIntersection() orderedIntersection()
(continued) (continued)
// move forward as long as we have not // reached the end of either set
while (lhsValue != null && rhsValue != null) {
if (lhsValue.compareTo(rhsValue) < 0) // lhsValue < rhsValue; move to // next value in lhs
lhsValue = advance(lhsIter);
else if (rhsValue.compareTo(lhsValue) < 0) // rhsValue < lhsValue; move to
// next value in rhs
rhsValue = advance(rhsIter);
orderedIntersection() orderedIntersection()
(concluded) (concluded)
else {
// lhsValue == rhsValue; add it // to intersection and move to // next value in both sets
setIntersection.add(lhsValue);
lhsValue = advance(lhsIter);
rhsValue = advance(rhsIter);
} }
return setIntersection;
}
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Complexity of Complexity of
orderdIntersection() orderdIntersection()
Assume the n Assume the n
lhslhsand n and n
rhsrhsare the number of are the number of elements in setA and setB. Each iteration elements in setA and setB. Each iteration
of the loop makes one or two comparisons of the loop makes one or two comparisons
which we assume occur in O(1) running which we assume occur in O(1) running
time. We must make at most n
time. We must make at most n
lhslhs+ n + n
rhsrhscomparisons, so the algorithm has worst- comparisons, so the algorithm has worst-
case running time O(n
case running time O(n
lhslhs+ n + n
rhsrhs). The ). The
algorithm is linear with respect to the total algorithm is linear with respect to the total
number of elements in the two sets.
number of elements in the two sets.
Maps Maps
A map stores data in entries, which are A map stores data in entries, which are
key value pairs. A key serves like an array ‑ key value pairs. A key serves like an array ‑
index to locate the corresponding value in index to locate the corresponding value in
the map. As a result, we call a map an the map. As a result, we call a map an
associative array.
associative array.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
The Map Interface The Map Interface
The methods size(), isEmpty() and The methods size(), isEmpty() and
clear() are identical to those of the Collection clear() are identical to those of the Collection
interface.
interface.
Methods containsKey() and remove() use only the Methods containsKey() and remove() use only the key as an argument.
key as an argument.
Methods get() and put() access and modify a value Methods get() and put() access and modify a value using the key.
using the key.
A map does not have an iterator to scan its elements. A map does not have an iterator to scan its elements.
Two methods keySet() and entrySet() return the keys and Two methods keySet() and entrySet() return the keys and the entries in a map as a set.
the entries in a map as a set.
The Map Interface (concluded)
The Map Interface (concluded)
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
TreeMap Class TreeMap Class
TreeMap is an ordered collection that TreeMap is an ordered collection that
accesses elements in the ascending order accesses elements in the ascending order
of its keys. The class implements the of its keys. The class implements the
OrderedMap interface that includes the OrderedMap interface that includes the
methods firstKey() and lastKey() which methods firstKey() and lastKey() which
return the value corresponding to the return the value corresponding to the
minimum and maximum key respectively.
minimum and maximum key respectively.
TreeMap Class (continued)
TreeMap Class (continued)
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
TreeMap Class (concluded) TreeMap Class (concluded)
// arrays for three classes and their enrollment
String[] className = {"ECON 101","CS 173","ENGL 25"};
int[] enrollment = {85,14, 30};
// create a TreeMap object
TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
// the key argument is a string from className and the value // argument is the corresponding Integer object from enrollment for(int i = 0; i < 3; i++)
tm.put(className[i], enrollment[i]);
Program 19.3 Program 19.3
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import ds.util.TreeMap;
import ds.time.Time24;
public class Program19_3 {
public static void main(String[] args) {
// a TreeMap object whose entries are a student // name and the total hours worked during a
// week; use a Time24 object for the value // component of an entry
TreeMap<String, Time24> timecardMap = new TreeMap<String,Time24>();
Time24 workTime, timeValue;
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.