C SC 205 Lecture 16: Tree Maps and Tree Sets
major resources: Data Structures and the Java Collections Framework Second Edition,
William Collins, McGraw-Hill, 2005
Introduction to Programming and OO Design
using Java, Niño and Hosch, Wiley & Sons, 2002
[ previous
| schedule
| next ]
Wednesday November 7 (week 9)
The Map concept and java.util.Map interface
- A map is a collection
- Each element in a map has two components: a unique key and a value
- The "mapping" is from the key to the value
- Values are retrieved based on their key
- The concept is that of a dictionary -- each word is unique and has associated meaning, and lookup is based on word
- The Map interface is part of the Java Collections Framework
- It does not extend the Collection interface!
- This is because it and some of its methods are parameterized with both key and value classes
- It is really the Map<K,V> interface and has no subinterface
- Since there is no subinterface, it does not extend Iterable (Collection does)
- Therefore there is no defined iterator over a map
- There is also no defined order (at the Map specification level)
- You may however obtain a "collection view" of a map and iterate over it
- Map was introduced in Java 1.2 to replace the pure abstract Dictionary class
- Dictionary remains, and has been parameterized with <K,V>, but is tagged obsolete.
The Map<K,V> specification
- For details, see
http://math.otterbein.edu/home/java/j2sdk1.5/docs/api/java/util/Map.html
- V put(K key, V value) -- Action depends on whether an element with the specified key
is already in the map. If not, a new element containing the key and value is created and added
and null returned. If so, the given value replaces the previous value and the latter is returned.
- V get(Object key) -- Returns null if no map element has this key or if this key's associated
value is null. Use containsKey() (below) to distinguish between the two.
- V remove(Object key) -- Removes the element (key-value pair) from map and returns value. As
with get(), return value null could mean one of two things.
- boolean containsKey(Object key) -- Does what you would expect.
- boolean containsValue(Object value) -- Also does what you would expect. Note that more
than one element can contain the same value
- Collection<V> values() -- Returns a "collection view" of the values stored in the map. This allows you
to iterate over the values. If a value is removed from the collection using collection or Iterator
removal methods, its corresponding mapping (element) will be removed from the map as well! However you cannot add to the
map by adding to the collection, since adding to the map requires a key and the collection does not have them.
- Set<K> keySet() -- Returns a "collection view" of the keys in this map as a Set. A set cannot
contain duplicates (but it can contain one null). The Set interface, like Collection,
extends Iterable, so once you obtain the key set you can iterate over it. Removal works same as for
values().
- Set<Map.Entry<K,V>> entrySet() -- This gives you a "collection view" of the map entries (elements) as a Set.
Map.Entry<K,V>> is defined inside Map<K,V>, and represents
a key-value pair. Iteration and removal work as for keySet().
The AbstractMap<K,V> class
- Provides skeletal implementation of Map interface.
- Only entrySet() is left abstract
- But put() throws UnsupportedOperationException
The SortedMap<K,V> subinterface
- It extends Map<K,V>
- Guarantees that iterator defined over a "collection view" of the map will iterate in ascending key order
- Provides access to Comparator if one is being used instead of the "natural order" (Comparable) of keys
The TreeMap<K,V> class
- Extends AbstractMap<K,V>
- Implements SortedMap<K,V> (and thus Map<K,V> also)
- Includes a constructor that takes Comparator argument
- If comparator is provided, it will be used instead of natural order
- Implemented using a Red-Black tree
- It meets specification of SortedMap, thus any iterator over a collection view will return
values in ascending order of associated key
- Its put() method will attach new Entry as BST leaf on the tree, then call
private fixAfterInsertion() method
- "Path rule" is not an immediate problem since new node is Red and so does not add Black node to path.
- Need to ensure "Red rule". New nodes are added as Red so to meet the rule, its parent cannot be red.
- May have to change parent color to ensure Red rule and this can cause Path rule violation, and if so that has to be addressed.
- Its deleteEntry() method will remove Entry as it would for BST node, then call
private fixAfterDeletion() method to rebalance.
Using a TreeMap to sort an existing list
Beware: the list can contain duplicate values but the map cannot contain duplicate keys!
Handle this by associating a "count" value with each key -- the number of times it appears in list
- Iterate through list and for each element:
- Determine if this element is already a key in the map
- If not, add it with count of 1 as its value
- If so, increment the matching map entry's count value
- Clear the list
- Get an entrySet() (collection view) for the map
- Iterate through the entry set and for each entry:
- Loop for "count" times (the value for that entry)
- For each loop iteration, add the key to the list
Think about the running time of this sort! What is it?
Set, SortedSet, AbstractSet, TreeSet
- Part of Java Collections Framework
- A set does not allow duplicate elements (can have one null)
- Like the list classes, and unlike the map classes, these are based on Collection and Iterable
- Similar relationships to each other as Maps have
- Set<E> is interface that extends Collection<E> and Iterable<E>
- SortedSet<E> is interface that extends Set<E>
- AbstractSet<E> is abstract class that extends AbstractCollection<E> and implements Set<E>
- TreeSet<E> is class that extends AbstractSet<E> and implements SortedSet<E>
- By implementing SortedSet, TreeSet assures that the iterators it provides will
return elements in ascending order according to natural order (or comparator, if provided to TreeSet constructor)
- TreeSet implementation uses a TreeMap instance variable to hold the collection!
- Result is logarithmic time complexity for the critical operations of adding and removing values
- Recall that TreeMap entries contain Key-Value pair
- The TreeSet element is stored as the key
- PRESENT (a static final Object) is stored as the value for all entries!
[ C
SC 205 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)