03 Collections2
03 Collections2
03 Collections2
1
Priority queue ADT
• priority queue: a collection of ordered elements that provides fast
access to the minimum (or maximum) element
usually implemented using a tree structure called a heap
4
The Map ADT
• map: Holds a set of unique keys and a collection of values, where
each key is associated with one value.
a.k.a. "dictionary", "associative array", "hash"
remove(key): Removes
the given key and its myMap.get("Juliet") returns "Capulet"
mapped value.
5
Map concepts
• a map can be thought of as generalization of a tallying array
the "index" (key) doesn't have to be an int
// (R)epublican, (D)emocrat,
(I)ndependent
count votes: "RDDDDRRRRRDDDDDDRDRRIRDRRIRDRRID"
"R" 14
key "R" "D" "I"
"D" 3
value 15 14 3
"I" 15
keys values
6
Map implementation
• in Java, maps are represented by Map interface in java.util
A map requires 2 type parameters: one for keys, one for values.
7
Map methods
put(key, value) adds a mapping from the given key to the given value;
if the key already exists, replaces its value with the given one
get(key) returns the value mapped to the given key (null if not found)
containsKey(key) returns true if the map contains a mapping for the given key
remove(key) removes any existing mapping for the given key
clear() removes all key/value pairs from the map
size() returns the number of key/value pairs in the map
isEmpty() returns true if the map's size is 0
toString() returns a string such as "{a=90, d=60, c=70}"
8
Using maps
• A map allows you to get from one half of a pair to the other.
Remembers one piece of information about every index (key).
// key value
put("Joe", "206-685-2181")
Map
Later, we can supply only the key and get back the related value:
Allows us to ask: What is Joe's phone number?
get("Joe")
Map
"206-685-2181"
9
Maps vs. sets
• A set is like a map from elements to boolean values.
Set: Is Joe found in the set? (true/false)
"Joe" true
Set
false
10
keySet and values
• keySet method returns Set of all keys in map
can loop over the keys in a foreach loop
can get each key's associated value by calling get on the map
Map<String, Integer> ages = new TreeMap<String, Integer>();
ages.put("Joe", 57);
ages.put("Geneva", 2); // ages.keySet() returns Set<String>
ages.put("Vicki", 19);
for (String name : ages.keySet()) { // Geneva -> 2
int age = ages.get(name); // Joe -> 57
System.out.println(name + " -> " + age); // Vicki -> 19
}
14
Iterators (11.1)
• iterator: An object that allows a client to traverse the elements of
any collection.
Remembers a position, and lets you:
• get the element at that position
• advance to the next position
• remove the element at that position
index 0 1 2 3 4 5 6 7 8 9 "the"
list value 3 8 9 7 5 12 0 0 0 0 set "to" "we"
size 6 "from"
15
Iterator methods
hasNext() returns true if there are more elements to examine
next() returns the next element from the collection (throws a
NoSuchElementException if there are none left to examine)
remove() removes the last value returned by next() (throws an
IllegalStateException if you haven't called next() yet)
18
Iterators and linked lists
• Iterators are particularly useful with linked lists.
The previous code is O(N2) because each call on get must start from
the beginning of the list and walk to index i.
Using an iterator, the same code is O(N). The iterator remembers its
position and doesn't start over each time.
current element: -3
iterator
current index: 1
19
ListIterator
add(value) inserts an element just after the iterator's position
hasPrevious() true if there are more elements before the iterator
nextIndex() the index of the element that would be returned the next time
next is called on the iterator
previousIndex() the index of the element that would be returned the next time
previous is called on the iterator
previous() returns the element before the iterator (throws a
NoSuchElementException if there are none)
set(value) replaces the element last returned by next or previous with
the given value
ListIterator<String> li = myList.listIterator();