Let’s see some Set<E>‘s methods declarations.
public boolean add(E e);
public E get(int index);
And let’s try to use this.
List<Boolean> list = new ArrayList<Boolean>();
Integer i = list.get(0); //Predictably, here we get a compile error.
list.contains(new Integer(3)); //But this line is allowed. Why?
Even in non-generic equivalent of this code (as I know, it’ll simply transformed into it), we get compile errors in both lines.
List s = new ArrayList();
s.contains((Boolean)(new Integer(3)));
Integer i = (Boolean)s.get(3);
So why do not I get the error in the generic case?
containsis not generified. Its signature remains:http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#contains(java.lang.Object)
here’s some more:
http://smallwig.blogspot.com/2007/12/why-does-setcontains-take-object-not-e.html