4
In the coding below, because the Box<? extends T>
extends and the EqualityComparator<? super T>
super T? uses so as to make part of the code println
true return?
public boolean containsSame
(Box<? extends T> other, EqualityComparator<? super T> comparator){
return comparator.compare(get(),other.get());
}
public interface EqualityComparator<T> {
public boolean compare(T first, T second);
}
Box<Number> nBox = new Box<Number>(42);
Box<Integer> iBox = new Box<Integer>(42);
EqualityComparator<Object> sameObject = new EqualityComparator<Object>() {
public boolean compare(Object o1, Object o2) { return o1 == o2;}
};
System.out.println(nBox.containsSame(iBox, sameObject));
Related: Add elements to a List<? extends Number>
– Icaro Martins
I read this post in English but could not understand/relate to the example I posted above
– Pirategull
I think this one might help, too: What is the purpose of the super command when used in the parameter declaration of a method?
– Gustavo Sampaio
I was reading exactly this post, even so, I can not understand the example I posted. So I can use . get() on a generic list I need it to be type <? Super T> ?
– Pirategull