1 / 17

Sets and Maps

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

keelia
Télécharger la présentation

Sets and Maps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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<E>

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

  8. 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);

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

  12. Methods of Map < K, V >

  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. 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

  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’)* 312+ 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()

More Related