IT1815
Sets and Maps 3. To determine the union, intersection, and difference:
Sets
• A set is a collection of elements where each element is
unique.
• Common set operations:
o Union: The union of two (2) sets (A ∪ B) is the set that
contains all the elements in either set.
Example: {1, 3, 5, 7} ∪ {2, 3, 4, 5} = {1, 2, 3, 4, 5, 7}
o Intersection: The intersection of two (2) sets (A ∩ B)
is the set that contains only the elements common to
both sets.
Example: {1, 3, 5, 7} ∩ {2, 3, 4, 5} = {3, 5} Output:
o Difference: The difference of sets A and B (A – B) is Union: [Marco, Nika, Mairo, John, Mark, Kae]
the set that contains the elements that are in A but not Intersection: [Mark]
in B. Difference: [Nika, Mairo, Kae]
Example: {1, 3, 5, 7} – { 2, 3, 4, 5} = {1, 7} 4. To determine whether a set is a subset of another set:
o Subset: Set A is a subset of set B (A ⊂ B) if every System.out.println(a.containsAll(b));
element of set A is also an element of set B. • Curly braces or the set() function can be used to implement
Example: {1, 3, 5, 7} ⊂ {1, 2, 3, 4, 5, 7} = true sets in Python.
• Java contains three (3) general-purpose set implementations. • Sample codes for sets in Python:
All are included in the java.util package. 1. To create an empty set:
o HashSet – This stores its elements in a hash table a = set()
without a guaranteed order upon iteration. This is the 2. To initialize a set:
best-performing implementation. a = set(["Mark", "Nika", "Mairo", "Kae"])
o TreeSet – This stores its elements in a special type b = {"John", "Marco", "Mark"}
of tree where elements are sorted (natural or custom) #Equivalent to multiple use of add()
during iteration. 3. To determine the union, intersection, and difference:
o LinkedHashSet – This stores its elements in a hash
table with a linked list running through it. The order of
the elements during the iteration is the same as the
order they were inserted into the set.
• Sample codes for sets in Java:
1. To create an empty set:
Set a = new HashSet(); 4. To determine whether a set is a subset of another set:
Set b = new TreeSet (); print(a.issubset(b))
Set c = new LinkedHashSet();
2. To add items to the set:
Collections.addAll(a, "Mark", "Nika", "Mairo", "Kae");
//equivalent to multiple use of add()
08 Handout 1 *Property of STI
student.feedback@sti.edu Page 1 of 2
IT1815
Maps • Maps in Python are known as dictionaries. To create a
• A map is a set of ordered pairs where elements are known as dictionary, use the colon symbol (:) for key-value pairs within
keys (identifiers) and values (content). the curly braces.
o A map cannot contain duplicate keys. • Sample codes for dictionaries in Python:
o Each key can map to only one (1) value. 1. To create an empty dictionary:
Example (A map of first names): name_map = { }
Key Value 2. To initialize a dictionary:
N Nika name_map = {"M1": "Mark", "M2": "Mairo"}
K Kae 3. To insert a mapping:
M1 Mark name_map["N"] = "Nika"
M2 Mairo 4. To delete the mapping for a key:
• Same with sets, there are also three (3) general-purpose map del name_map["M2"]
implementations in Java: HashMap, TreeMap, and 5. To replace a key’s value:
LinkedHashMap. name_map["M2"] = "Marco"
• Sample codes for maps in Java: 6. To retrieve the value based on the key:
1. To create an empty map: print(name_map["M1"])
Map <String, String> nameMap = new HashMap<>(); 7. To check whether a dictionary contains a mapping for the
2. To insert a mapping: specified key:
nameMap.put("M1", "Mark"); print("M2" in name_map)
nameMap.put("M2", "Mairo"); 8. To retrieve all the keys and values of a dictionary:
3. To delete the mapping for a key: print(list(name_map))
nameMap.remove("M2"); print(list(name_map.values()))
4. To replace a key’s value: 9. To display entries in separate lines:
nameMap.replace("M2", "Marco"); for x, y in name_map.items():
5. To retrieve the value based on the key: print(x, y)
System.out.println(nameMap.get("M1"));
6. To check whether a map contains a mapping for the
References:
specified key or value: Koffman, E. and Wolfgang, P. (2016). Data structures: Abstraction and design using
System.out.println(nameMap.containsKey("M2")); Java. Hoboken: John Wiley & Sons, Inc.
System.out.println(nameMap.containsValue("Mark")); Oracle Docs (n.d.). Citing sources. Retrieved from
7. To retrieve all the keys and values of a map: https://docs.oracle.com/javase/8/docs/api/java/util/package-summary.html
System.out.println(nameMap.keySet()); Python Software Foundation (n.d.). The Python tutorial. Retrieved from
https://docs.python.org/3/tutorial/index.html
System.out.println(nameMap.values());
8. To display entries in separate lines:
for (Map.Entry e : nameMap.entrySet()) {
System.out.println(e.getKey() + ": " +
e.getValue());
//Map.Entry is an interface to retrieve entries
08 Handout 1 *Property of STI
student.feedback@sti.edu Page 2 of 2