Java Reference
In-Depth Information
The existence of a
Comparator
implies that all elements in a SortedSet collection
must be comparable with each other. To do this, either the
compareTo
method of the
element (that is,
element1.compareTo (element2)
) or the
compare()
method of the
Comparator
(that is,
Comparator.compare()
) will be used.
E
XERCISES
: J
AVA
C
OLLECTIONS
It's time to visit the example classes and try out all these new ideas. You'll start with
the Set collection type and then visit the List and the Map type.
1. Create a new class called
Pupil
that looks like the following class. You will
use this class for the exercises with
ArrayList
and
HashMap
.
public class Pupil
{
protected String name;
protected String grade;
public Pupil(String name, String grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public String getGrade() {
return grade;
}
public void setName(String name) {
this.name = name;
}
public void setGrade(String grade) {
this.grade = grade;
}
}