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

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

description

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

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

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

Sets and Maps

Ellen WalkerCPSC 201 Data Structures

Hiram College

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

Sets in the Collection Hierarchy

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

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

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

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

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

Methods of Set<E>

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

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

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

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

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

Compute Union and Intersection

HashSet<String> inBoth = (HashSet<String>)students356.clone();

inBoth.retainAll(students201); //intersectionSystem.out.println("Students in both 201 and 356 are: " +

inBoth);

HashSet<String> inOne = (HashSet<String>)(students356.clone());

inOne.addAll(students201); //unionSystem.out.println("Students in one of the classes are: "

+ inOne);

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

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

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

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

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

Graphical View

• CPSC• CS• MATH• INTD

• Computer Science• Mathematics• Interdisciplinary

Arrows connect key to valueThis is a many-to-one mapping (vs. one-to-one)

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

Methods of Map < K, V >

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

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

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

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)

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

Implementing Maps and Sets

• TreeMap and TreeSet– Implemented using Binary Search Tree (actually a

red-black tree)• HashMap and HashSet

– Implemented using Hash Table

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

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

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

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