ap computer science chapter 15 solutions

advertisement
Lesson 15 — Unordered Collections: Sets and Maps
Using Abstract Data Types
Lesson 15—Unordered Collections: Sets and Maps
Solutions
Answers to Exercises
Exercise 15.1
The items in a set do not have positions. Also, the items in a set are unique.
a. {"4" "3"}
b. {"4" "3"}
c. {"4" "3" "5"}
d. {"4" "5"}
You visit the items in a set by using an iterator.
// The difference of set a and set b is the set of items in
// a that are not also in b
Set difference(Set a, Set b){
Set result = new HashSet();
Iterator iter = a.iterator();
while (iter.hasNext()){
Object obj = iter.next();
if (! b.contains(obj))
result.add(obj);
}
return result;
}
// Is set a a subset of set b?
boolean subset(Set a, Set b){
Iterator iter = a.iterator();
while (iter.hasNext()){
Object obj = iter.next();
if (! b.contains(obj))
return false;
}
return true;
}
// Return an array of objects in a set
Object[] toArray(Set s){
Iterator iter = s.iterator();
Object[] array = new Object[s.size()];
for (int i = 0; i < array.length; i++)
array[i] = iter.next();
return array;
}
1
Lesson 15 — Unordered Collections: Sets and Maps
Using Abstract Data Types
To work with a set of integers, you have to wrap each int value in a new Integer object
before inserting it into the set. You also have to cast the set's items to Integer objects
and then extract the ints using the method intValue() for access.
Exercise 15.2
Items in a sorted set must be comparable so the implementation can place them in a
natural ordering.
The first restriction on the use of a sorted set to sort a list is that the items in the list must
be comparable. The second restriction is that the items in the list must be unique.
Exercise 15.3
// Return number of instance of item in a list
int occurrencesOf(Object item, List list){
int count = 0;
for (int i = 0; i < list.size(); i++)
if (list.get(i).equals(item))
count++;
return count;
}
Exercise 15.4
A set contains unique items in no particular order. A map associates a set of keys with
values.
When a key is used for inserting a value in a map and that key is already present, the new
value replaces the value currently associated with that key and the old value is returned.
A map is an appropriate data structure for representing any information that must be
accessed by content, not position. One appropriate use of a map would be in a keyed
reference manual, where the keys are the terms and the values are descriptions of these
terms. Another application of a map would be a phone book, where the keys are persons’
names and the values are their phone numbers.
Exercise 15.5
// Display the keys and values in two columns
void displayContents(Map map){
Set keys = map.keySet();
Iterator iter = keys.iterator();
while (iter.hasNext()){
Object key = iter.next();
Object value = map.get(key);
System.out.println(key + " " + value);
}
}
Exercise 15.6
2
Lesson 15 — Unordered Collections: Sets and Maps
Using Abstract Data Types
All collections have a size, contain objects, and can grow or shrink as items are added or
removed.
Review Questions
Vocabulary Review
Association: A pair of items consisting of a key and a value.
Association list: A list in which each value is associated with a unique key.
Dictionary: An unordered collection in which each value is associated with a unique key.
Hashing: A method by which the position of an item in a collection is related to the item's
content and can be determined in constant time.
Key: An item that is associated with a value and is used to locate this value in a collection.
Keyed list: An unordered collection in which each value is associated with a unique key.
Map: An unordered collection in which each value is associated with a unique key.
Mututally comparable: A property of two items such that they can be related by less than,
greater than, or equal to.
Set: An unordered collection of unique items.
Sorted map: A type of map that allows clients to visit its keys in sorted order.
Sorted set: A type of set that allows clients to visit its items in sorted order.
Value: An items that is associated with a key and is located by a key in a collection.
Fill in the Blank
1. The intems in an unordered collection have no particular positions.
2. The set of items that two other sets have in common is called their intersection.
3. The set of items that two other sets have altogether is called their union.
4. The set of items in the first set that are not present in the second set is called their
difference.
5. You visit all the items in a set using an iterator.
6. A map contains associations of keys and values.
7. To insert, remove, or access a value in a map, one uses the value’s key.
8. A sorted map is a type of map in which you can visit items in sorted order.
9. The Collection methods work with either lists or sets.
10. The Collections class provides a set of static methods for tasks ssuch as sorting and
searching.
3
Lesson 15 — Unordered Collections: Sets and Maps
Using Abstract Data Types
4
Download