Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sets and Maps Ellen Walker CPSC 201 Data Structures Hiram College.

Similar presentations


Presentation on theme: "Sets and Maps Ellen Walker CPSC 201 Data Structures Hiram College."— Presentation transcript:

1 Sets and Maps Ellen Walker CPSC 201 Data Structures Hiram College

2 Sets in the Collection Hierarchy

3 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 –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

4 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

5 Methods of Set

6 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);

7 Create and Print a Set HashSet students201 = new HashSet (); 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);

8 Compute Union and Intersection HashSet inBoth = (HashSet )students356.clone(); inBoth.retainAll(students201); //intersection System.out.println("Students in both 201 and 356 are: " + inBoth); HashSet inOne = (HashSet )(students356.clone()); inOne.addAll(students201); //union System.out.println("Students in one of the classes are: " + inOne);

9 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);

10 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) }

11 Graphical View CPSC CS MATH INTD Computer Science Mathematics Interdisciplinary Arrows connect key to value This is a many-to-one mapping (vs. one-to-one)

12 Methods of Map

13 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

14 Corresponding Map and Set Methods 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)

15 Implementing Maps and Sets TreeMap and TreeSet –Implemented using Binary Search Tree (actually a red-black tree) HashMap and HashSet –Implemented using Hash Table

16 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’)* 31 2 + hash(‘A’)*31 + hash(‘T’) –Hash(“EAT”) ≠ hash(“ATE”) ≠ hash(“TEA”)

17 .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()


Download ppt "Sets and Maps Ellen Walker CPSC 201 Data Structures Hiram College."

Similar presentations


Ads by Google