Let’s assume that we have a class Pair with two fields and we want to print a Pair instance object. Once we call the print method we will obtain something like Pair@10a5
class Pair<A, B> {
A first;
B last;
public Pair(A first, B last) {
this.first = first;
this.last = last;
}
public A getFirst() {
return first;
}
public void setFirst(A first) {
this.first = first;
}
public B getLast() {
return last;
}
public void setLast(B last) {
this.last = last;
}
}
That’s because in java every class derives from Object class and every time we print an instance of a class the default toString from Object class is called. The default implementation is composed of two main parts Type and HashCode. So for our Pair@10a5 we have the Type = Pair and the hashCode = 10a5.
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
If we want to print something with a more clear representation we need to override the toString method in the Pair class.
@Override
public String toString() {
return "Pair [first=" + first + ", last=" + last + "]";
}
In this way from the main of our class we can have ArrayList of Pair and print it. For each instance the overridden toString() method will be called instead of the one from the Object class.
public static void main(String[] args) {
List<Pair<Long, Long>> pairsList =
Arrays.asList(new Pair(100L, 200L), new Pair(300L, 400L));
System.out.println(pairsList);
}
-- > output
[Pair [first=100, last=200], Pair [first=300, last=400]]
Another interesting thing we need to take in consideration are the equals and hashCode methods.
Let’s assume that we have a Set of Integer and we add the number “1” twice. The second add it’s not performed because the Set data structure detects that there is already a number “1” inserted.
We would expect that the same would happen if we had a Set of Pair object, right? But in the second case we are able to add both Pair even if they are equals.
Set<Integer> intSet = new HashSet<Integer>();
intSet.add(1);
intSet.add(1);
System.out.print(intSet);
--> output
[1]
Set<Pair<Long, Long>> pairList = new HashSet<Pair<Long, Long>>();
pairList.add(new Pair(100L, 200L));
pairList.add(new Pair(100L, 200L));
System.out.print(pairList);
--> output
[Pair [first=100, last=200], Pair [first=100, last=200]]
This happens because in order to compare if two elements are equal the default equals and hashCode method from Object class are called. The Default implementation of these method will tell us:
- equals(Object obj): if an object passed as argument is “equal to” the current instance and as default two objects are equal if and only if they are stored in the same memory address.
- hashcode(): returns an integer representation of the object memory address. By default, this method will return a random integer that is unique for each instance and might change is we run multiple time the same application.
In order to make our Set understand how to deal with Pair object we need to override the equals and hashCode method in our Pair class. The complete implementation of our Pair class
class Pair<A, B> {
A first;
B last;
public Pair(A first, B last) {
this.first = first;
this.last = last;
}
public A getFirst() {
return first;
}
public void setFirst(A first) {
this.first = first;
}
public B getLast() {
return last;
}
public void setLast(B last) {
this.last = last;
}
@Override
public String toString() {
return "Pair [first=" + first + ", last=" + last + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((first == null) ? 0 : first.hashCode());
result = prime * result + ((last == null) ? 0 : last.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (first == null) {
if (other.first != null)
return false;
} else if (!first.equals(other.first))
return false;
if (last == null) {
if (other.last != null)
return false;
} else if (!last.equals(other.last))
return false;
return true;
}
}
Now if we ran again our main method we will only see one of the pair.
Set<Pair<Long, Long>> pairList = new HashSet<Pair<Long, Long>>();
pairList.add(new Pair(100L, 200L));
pairList.add(new Pair(100L, 200L));
System.out.print(pairList);
--> output
[Pair [first=100, last=200]]