Java Reference
In-Depth Information
if(count == null) {
keywords.put(keyword, 1);
} else {
keywords.put(keyword, count + 1);
}
}
}
The keywordFound method takes in a String object to represent a keyword. If the String
is not in the map, then count is null and the keyword is added with a value of 1. If the
keyword is in the map already, it is replaced with the same keyword but an incremented
value. The following statements create a new KeywordCounter and add various strings to
the map:
KeywordCounter webpage = new KeywordCounter();
webpage.keywordFound(“java”);
webpage.keywordFound(“ejb”);
webpage.keywordFound(“java”);
webpage.keywordFound(“jsp”);
for(String keyword : webpage.keywords.keySet()) {
System.out.println(keyword + “ = “ +
webpage.keywords.get(keyword));
}
The output of this code is
ejb = 1
jsp = 1
java = 2
The Comparable Interface
The Comparable interface in the java.util package creates an ordering for a collection of
objects by providing a method to compare two objects. The Comparable interface contains
only one method:
public int compareTo(T o)
Search WWH ::




Custom Search