Title: Sets and Maps
1Sets and Maps
2Set Interface
- Models collection with no repetitions
- subinterface of Collection
- has all collection methods
- has a subinterface SortedSet
- items stored must implement Comparable OR
- must have Comparator supplied at creation
- same as set except iterator will traverse items
in increasing order - has extra methods based on order
3Set, basic methods
- boolean add(Object obj)
- adds obj to set, returns false if duplicate
- boolean contains(Object obj)
- true if obj is in this set, else false
- boolean remove(Object obj)
- if obj is in this set removes it and returns true
, else returns false - int size()
- Iterator iterator()
- returns an iterator for this set
4Set, other methods
- boolean isEmpty()
- void clear()
- addAll(Collection stuff)
- containsAll(Collection stuff)
- removeAll(Collection stuff)
- retainAll(Collection stuff)
5Set example- method intersectionusing basic
methods, iterator
public static Set intersect(Set setA, Set
setB) Set aSet new HashSet()
Iterator itr setA.iterator()
while(itr.hasNext()) String str
(String)itr.next() if(setB.contains(str))
aSet.add(str) return aSet
6Set example- method unionusing basic methods,
iterator
public static Set union(Set setA, Set setB)
Set aSet new HashSet() Iterator itr
setA.iterator() while(itr.hasNext())
aSet.add(itr.next()) itr
setB.iterator() while(itr.hasNext())
aSet.add(itr.next()) return aSet
7Set example- method intersectionusing other
methods
public static Set intersect2(Set setA, Set
setB) Set aSet new HashSet(setA)
aSet.retainAll(setB) return aSet
8Set example- method unionusing other methods
public static Set union2(Set setA, Set setB)
Set aSet new HashSet(setA)
aSet.addAll(setB) return aSet
9Set - implementations
- HashSet
- requires method hashCode for objects stored
- inherited from Object, but should be overridden
- must be consistent with equals method
- uses hash table
- expected time for add, remove, contains all O(1)
- depends on "good" hashing function (hashCode)
10Set - implementations
- TreeSet
- implements SortedSet
- items must implement Comparable OR
- must have Comparator supplied when constructed
- uses a balanced binary search tree
- time for add, remove, contains is O(log N) for
set containing N items - iterator traverses elements in increasing order
- other order-based methods available
11Set - implementations
- Compare results of sample code for HashSet and
TreeSet - sets are printed in arbitrary order for HashSet
- sets are printed in order for TreeSet
12Set - exercises
- in Marine Biology Simulation
- Rewrite an Environment implementation (any one
will do) so that the method allObjects returns a
set of Locatable objects. Then rewrite the
Simulation method step to use a set with
iterator. (Note, this will break the given
implementations of EnvDisplay -- you must use a
special version.)
13Map interface
- Not a sub-interface of collection
- Defines a map or directory
- Store a pair of a key and an item
- example key is name, item is address and/or
phone number - Look up items by using key
14Map interface - basic methods
- Object put(Object key, Object value)
- stores value under this key, returns previous
value stored - Object get(Object key)
- gets the value stored under key
- boolean containsKey(Object key)
- returns true if some value is stored under this
key - Set keySet()
- returns a Set containing all keys
- size()
15Map interface - other methods
- boolean isEmpty()
- Object remove(Object key)
- removes the key-value pair for this key and
returns the value stored, or null - void clear()
- Collection values()
- returns a collection with all values from this
map, including duplicates
16implementations of Map
- HashMap
- same requirements on keys as for HashSet elements
- hashCode method appropriate
- uses hash table
- O(1) average time for look-ups, puts, removes
- TreeMap
- same requirements on keys as for TreeSet elements
- needs elements Comparable or Comparator to
constructor - uses balanced binary search tree
- O(log N) time for look-ups, puts, removes
- keySet() method returns a TreeSet (SortedSet)
17ExampleSets of cities associated with states
- class CitiesByState encapsulates the map and
operations - keys are names of states, as Strings
- values are sets
- each set contains one or more cities as Strings
- uses CityInfo type (interface)
- String city() -- returns name of city
- String state() -- returns name of state
18ExampleSets of cities associated with states
- class CitiesByState has private instance variable
myMap to store information - operations
- constructor takes BufferedReader to create
initial map - void addCity(CityInfo info)
- adds city with associated state from info
- void printState()
- prints state name followed by all city names
under this state - void printAllStates()
- prints information for all states
19Example
public void addCity(CityInfo info) String
state info.state() Set citySet
if(myMap.containsKey(state)) citySet
(Set)myMap.get(state) else citySet
new HashSet() myMap.put(state, citySet)
citySet.add(info.city())
20Example
public void printState(String state)
Set citySet (Set)myMap.get(state) Iterator
itr citySet.iterator() System.out.print(st
ate " ") while(itr.hasNext())
System.out.print(itr.next() ", ")
System.out.println()
21Example
public void printAllStates() Set
stateSet myMap.keySet() Iterator itr
stateSet.iterator() while(itr.hasNext())
printState(itr.next())
22Example
- Change HashMap to TreeMap (in constructor) to see
the difference - Change HashSet to TreeSet (in addCity method) to
see the difference
23Exercise - Environment forMarine Biology
Simulation
- Store Locatable Objects using a Map
- key is location
- value is Fish, or other Locatable object
- Reimplement methods that access the storage data
structure