Java Reference
In-Depth Information
9 map.put(
"Smith"
,
65
);
10
11 System.out.println(
"Entries in map: "
+ map);
12
13 System.out.println(
"The age for Lewis is "
+
14 map.get(
"Lewis"
));
15
16 System.out.println(
"Is Smith in the map? "
+
17 map.containsKey(
"Smith"
));
18 System.out.println(
"Is age 33 in the map? "
+
19 map.containsValue(
33
));
20
21 map.remove(
"Smith"
);
22 System.out.println(
"Entries in map: "
+ map);
23
24 map.clear();
25 System.out.println(
"Entries in map: "
+ map);
26 }
27 }
display entries
get value
is key in map?
is value in map?
remove entry
Entries in map: [[Anderson, 31][Smith, 65][Lewis, 29][Cook, 29]]
The age for Lewis is 29
Is Smith in the map? true
Is age 33 in the map? false
Entries in map: [[Anderson, 31][Lewis, 29][Cook, 29]]
Entries in map: []
The program creates a map using
MyHashMap
(line 4) and adds five entries into the map
(lines 5-9). Line 5 adds key
Smith
with value
30
and line 9 adds
Smith
with value
65
. The
latter value replaces the former value. The map actually has only four entries. The program
displays the entries in the map (line 11), gets a value for a key (line 14), checks whether the
map contains the key (line 17) and a value (line 19), removes an entry with the key
Smith
(line 21), and redisplays the entries in the map (line 22). Finally, the program clears the map
(line 24) and displays an empty map (line 25).
27.20
✓
✓
What is
1 << 30
in line 8 in Listing 27.2? What are the integers resulted from
1 << 1
,
1 << 2
, and
1 << 3
?
Check
Point
27.21
What are the integers resulted from
32 >> 1
,
32 >> 2
,
32 >> 3
, and
32 >> 4
?
27.22
In Listing 27.2, will the program work if
LinkedList
is replaced by
ArrayList
?
In Listing 27.2, how do you replace the code in lines 55-59 using one line of code?
27.23
Describe how the
put(key, value)
method is implemented in the
MyHashMap
class.
27.24
In Listing 27.5, the
supplementalHash
method is declared static, can the
hash
method be declared static?
27.25
Show the output of the following code.
MyMap<String, String> map =
new
MyHashMap<>();
map.put(
"Texas"
,
"Dallas"
);
map.put(
"Oklahoma"
,
"Norman"
);
map.put(
"Texas"
,
"Austin"
);
map.put(
"Oklahoma"
,
"Tulsa"
);
System.out.println(map.get(
"Texas"
));
System.out.println(map.size());
Search WWH ::
Custom Search