This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java 5 -Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Java 5 -Collection" Watch "Java 5 -Collection" New topic
Author

Java 5 -Collection

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8876

I want to know why the following Collection<E> interface methods, accept Collection<?> as the parameter and not Collection<? super E>

boolean containsAll(Collection<?> c)
boolean removeAll(Collection<?> c)
boolean retainAll(Collection<?> c)


If I create a Collection<String>, does invoking c.containsAll(m) (where m is Collection<Map> make sense? :roll: I dont think so. Could someone explain?
Thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This must be advanced stuff because this code:


compiles and runs without error. Mind you it doesn't do anything either.

It is rather all confusing
[ December 30, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26192
    
  66

Pradeep,
Suppose I have a the Dog-Animal-Object hierarchy. Obviously it makes sense to call collectionOfAnimals.containsAll(collectionOfDogs).

However, it also makes sense to call collectionOfDogs.containsAll(collectionOfAnimals). I may not know what is in my collectionOfAnimals. It is entirely possible that it contains only dogs (or is empty) and then the call makes sense.

Another reason is consistency. It must be valid to call collectionOfDogs.containsAll(collectionOfObjects) and collectionOfObjects.containsAll(collectionOfDogs) for the same reasons explained above. collectionOfObjects is equivalent to the pre-java 1.5 Collection. So I can get this functionality by using a Collection without specifying a type because java 1.5 is backward compatible.
[ December 30, 2004: Message edited by: Jeanne Boyarsky ]

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8876

Thanks Barry and Jeanne. Jeanne, your explanation was quite useful.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Java 5 -Collection
 
Similar Threads
Generics problem
Questions on Collection Interface.
Java 5 -Collection
Question about generics
Collections / Generics Question