Java Reference
In-Depth Information
9.
The
countDuplicates
code is identical to the solution provided for problem 4,
but the method's header should be changed to the following:
public static int countDuplicates(List<Integer> list) {
10.
You should use a
Set
rather than a
List
if you wanted to avoid duplicates or
wanted to be able to search the collection quickly.
11.
You should use a
TreeSet
when you want to keep the data in sorted natural order.
You should use
HashSet
s with non
Comparable
types or when order doesn't mat-
ter, to reduce the searching time to a minimum.
12.
You can examine every element of a
Set
using an iterator.
13.
[32, 90, 9, 182, 29, 12]
14.
[79, 8, 132, 50, 98, 86]
15.
[94, 4, 11, 84, 42, 12, 247]
16.
To do a union, use the
addAll
method to add one set's contents to the other. To do
an intersection, use the
retainAll
method to remove elements not common to
both sets.
17.
Map<String, Integer> ageMap = new TreeMap<String, Integer>();
ageMap.put("Stuart", 85);
ageMap.put("Marty", 12);
ageMap.put("Amanda", 25);
18.
You can examine every key of a
Map
by calling the
keySet
method and then iter-
ating or performing a for-each over the
keySet
. You can examine every value of a
Map
by calling the
values
method and then iterating or performing a for-each over
that collection of values, or by looking up each associated value using the keys
from the
keySet
.
19.
{17=Steve, 34=Louann, 15=Moshe, 2350=Orlando, 7=Ed, 5=Moshe,
27=Donald}
20.
{79=Seventy-nine, 8=Ocho, 132=OneThreeTwo, 50=Fifty,
98=Ninety-eight, 86=Eighty-six}
21.
The following method implements the new behavior in the
wordCount
program:
public static void reverseMap(
Map<String, Integer> wordCountMap) {
Map<Integer, String> reverseMap =
new TreeMap<Integer, String>();
// reverse the original map
for (String word : wordCountMap.keySet()) {
Search WWH ::
Custom Search