What the API says is:
- You cannot
add() anything that is not E ; - You, however, are allowed to look for things that are not
E (but this can be compared to an instance of E ).
Consider the following example:
public class Main { public static class Key { private final int k; public Key(int k) { this.k = k; } @Override public boolean equals(Object obj) { if (!(obj instanceof Key)) { return false; } Key rhs = (Key)obj; return k == rhs.k; } @Override public int hashCode() {
The last three lines will not compile if contains() was limited to accepting Data .
source share