I'm trying to write a generic class for training.
public class Pair<T> {
T first;
T second;
private Pair(T first, T second){
this.first=first;
this.second=second;
}
public T getFirst(){
return first;
}
public T getSecond(){
return second;
}
}
And I made the ide add equals and hash itself.As a result, she wrote this implementation:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?> pair = (Pair<?>) o;
return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
Since I haven't figured it out much yet, I don't understand this line:
Pair<?> pair = (Pair<?>) o;
Why <?>
? What kind of person will she put there and where will she take it from?
I understand that Pair<T> pair = (Pair<T>) o;
wrong, because there will be an erasure of the type , but what does<? >give us? I will be glad to help you figure it out, thank you.