170 likes | 291 Views
Sets and Maps. Ellen Walker CPSC 201 Data Structures Hiram College. Sets in the Collection Hierarchy. Set vs. List. A list is a sequence of items Order matters Traversal makes sense Duplicates are allowed in the list A set is a collection of items, without sequence
E N D
Sets and Maps Ellen Walker CPSC 201 Data Structures Hiram College
Set vs. List • A list is a sequenceof items • Order matters • Traversal makes sense • Duplicates are allowed in the list • A set is a collection of items, without sequence • Order does not matter; traversal does not make sense • No duplicates are allowed in the list • Like ordered lists and binary search trees, sets are value-oriented
Mathematical Set Operations • Element-of (x S) • Boolean: Is x a member of the set S? • Subset (S T) • Boolean: Are all elements of S also elements of T? • Union (S T) • Create a new set with all elements of S and all elements of T • Intersection (S T) • Create a new set with all elements that are in both S and T • Set-Difference (S – T) • Create a new set with all elements that are in S but not in T
Implementing Mathematical Operations • Element-of (x S) • S.contains(x); • Subset (S T) • T.containsAll(S); • Union (S T) • S.addAll(T); or T.addAll(S); • Intersection (S T) • S.retainAll(T) or T.retainAll(S); • Set-Difference (S – T) • S.removeAll(T);
Create and Print a Set HashSet<String> students201 = new HashSet<String>(); String[] names = {"ChrisK", "ChrisF", "Robin", "Tim", "Andrew", "David", "Will", "Matt"}; for(int i=0;i<names.length;i++) students201.add(names[i]); System.out.println("Students in 201 are: ” + students201);
Compute Union and Intersection HashSet<String> inBoth = (HashSet<String>)students356.clone(); inBoth.retainAll(students201); //intersection System.out.println("Students in both 201 and 356 are: " + inBoth); HashSet<String> inOne = (HashSet<String>)(students356.clone()); inOne.addAll(students201); //union System.out.println("Students in one of the classes are: " + inOne);
Set Iteration • Iterator presents elements of set in arbitrary order • Because there is an iterator, we can use the foreach loop with sets: for (String x : students201) System.out.println(x);
Maps • A map is a set of ordered (key, value) pairs • Given the key, we should be able to find the value • Example: • { (CPSC, Computer Science), (MATH, Mathematics), (INTD, Interdisciplinary) (CS, Computer Science) }
CPSC CS MATH INTD Computer Science Mathematics Interdisciplinary Graphical View Arrows connect key to value This is a many-to-one mapping (vs. one-to-one)
Maps and Sets are Similar… • In both, order doesn’t matter • For both, need to determine whether an object is present (key, for a map) • Duplicates are not allowed (duplicate keys not allowed for maps) • Maps are more complicated because the association between key and value must be maintained
Map Methods V get (Object key) V put(K key, V value) V remove (Object key) Set Methods boolean contains(Object key) boolean add (K key) boolean remove(K key) Corresponding Map and Set Methods
Implementing Maps and Sets • TreeMap and TreeSet • Implemented using Binary Search Tree (actually a red-black tree) • HashMap and HashSet • Implemented using Hash Table
Hash Codes in Java • Object.hashCode() • Based on address in memory • String.hashCode() • Multiply each character’s code by 31 raised to a power depending on position • Example hash(“EAT”) = hash(‘E’)* 312+ hash(‘A’)*31 + hash(‘T’) • Hash(“EAT”) ≠ hash(“ATE”) ≠ hash(“TEA”)
.hashCode() and .equals() • Java contract for .hashCode • If obj1.equals(obj2) then obj1.hashCode() == obj2.hashCode() • Object’s .equals and .hashCode() depend on address • Many objects (e.g. Integer, String) override both .equals() and .hashCode() to depend on value • If your class overrides .equals(), it should also override .hashCode()