I am searching for a solution that allows me to add Deques
of any inner type to my List
.
Now the following code is possible.
/*
* Please ignore the missing instantiations. It's only a technical
* precompiler demonstration.
*/
final Deque<String> concreteDeque = null;
final Deque<?> unclearDeque = null;
final List<Deque<?>> list = null;
/*
* The precompiler demands a parameter of type Deque<?>. That means any
* Deque. Great! :)
*/
list.add(concreteDeque); // no precompiler error
list.add(unclearDeque); // no precompiler error
final Deque<?> deque = list.get(0); // no precompiler error, great! :)
Now, I want to make my own more specific interface.
/**
* A list that holds instances of {@link Deque}. Only for demonstrations.
*
* @author Barock
*
* @param <DT>
* The type that should pass to the {@link Deque}.
*/
public interface DequeList<DT> extends List<Deque<DT>> { }
But with this interface my code doesn't work anymore.
final Deque<String> concreteDeque = null;
final Deque<?> unclearDeque = null;
final DequeList<?> dequeList = null;
// Now the precompiler announces wildcard capture for adding elements!?
dequeList.add(concreteDeque); // precompiler error
dequeList.add(unclearDeque); // precompiler error
final Deque<?> deque = dequeList.get(0); // still no precompiler error. :)
I assume that my DequeList<?>
is equivalent to the List<Deque<?>>
. Why obviously not?