2014-03-06

Generics: difference between a wildcard vs Object

The short version is:

Collection<Object> != (Collection<?, ?> == Collection)
or
Map<Object, Object> != (Map<?, ?> == Map)
Why? Using just Collection is — since Java 5 & the introduction of Generics — a short form for Collection<?> collectionOfUnknown which is not the same as Collection<Object> collectionOfObjects because you can assign any collection to collectionOfUnknown like Collection<Foo> collectionOfFoos, Collection<Bar> collectionOfBars or Collection<Object> collectionOfObjects but you cannot assign Collection<Foo> collectionOfFoos to a Collection<Object> collectionOfObjects.