Ch11 Java Collection Framework
Ch11 Java Collection Framework
2026
Empirical analysis
Running a program and measuring its performance
• How much time does it take to store Moby Dick into a List?
4
1 4
"the"
"if" "of"
[Link]("to") "to"
"down" "from" true
"by" "she"
[Link]("be") "you" false
"in"
"why" "him"
set
2 5
2 5
3 6
16.03.2026
7 10
"Marty" true
Set
false
addAll retainAll removeAll
addAll(collection) adds all elements from the given collection to this set
containsAll(coll) returns true if this set contains every element from given set
returns true if given other set contains the same elements
– Map: What is "Marty" 's phone number?
equals(set)
iterator() returns an object used to examine set's contents (seen later) "Marty" "206-685-2181"
removeAll(coll) removes all elements in the given collection from this set Map
retainAll(coll) removes elements not found in given collection from this set
toArray() returns an array of the elements in this set
8 11
8 11
9 12
16.03.2026
13 16
Reversing a map
• We can reverse the mapping to be from GPAs to names.
Map<Double, String> taGpa = new HashMap<Double, String>();
[Link](3.6, "Jared");
Iterators
[Link](4.0, "Alyssa");
[Link](2.9, "Steve");
[Link](3.6, "Stef");
[Link](2.9, "Rob");
...
[Link]("Who got a 3.6? " +
[Link](3.6)); // ??? reading: 11.1; 15.3; 16.5
14 17
15 18
16.03.2026
19 22
20 23
21 24
16.03.2026
• basic map operations: remove(key) removes any existing mapping for the given key
clear() removes all key/value pairs from the map
– put(key, value ): Adds a
mapping from a key to size() returns the number of key/value pairs in the map
a value. isEmpty() returns true if the map's size is 0
toString() returns a string such as "{a=90, d=60, c=70}"
– get(key ): Retrieves the
value mapped to the key. keySet() returns a set of all keys in the map
– remove(key ): Removes values() returns a collection of all values in the map
the given key and its putAll(map) adds all key/value pairs from the given map to this map
mapped value. equals(map) returns true if given map has the same mappings as this one
[Link]("Juliet") returns "Capulet"
25 28
25 28
// (M)cCain, (O)bama, (I)ndependent – Later, we can supply only the key and get back the related value:
– count votes: "MOOOOOOMMMMMOOOOOOMOMMIMOMMIMOMMIO" Allows us to ask: What is Marty's phone number?
"O" Map
value 16 14 3 3
"206-685-2181"
"I" 16
keys values 26 29
26 29
27 30
27 30
16.03.2026
31 34
Languages and Grammars • Some sentences that could be generated from this grammar:
Marty slept
Jessica belched
Stuart cried
35
32 35
33 36
33 36
16.03.2026
• Some sentences that could be generated from this grammar: <adj> <adjp>
the invisible carrot cried
Jessica belched <adj>
a computer slept
a romantic ball belched Fred honored the green wonderful child
37 40
37 40
38
38
39