Home Java Java-tips Data Collections Collection Interface

Ask Questions?

View Latest Questions


 
 

Collection Interface
Posted on: July 26, 2006 at 12:00 AM
The Collection interface is the parent of the List and Set interfaces, but not Map.

Java Notes

Collection Interface

The Collection interface is the parent of the List and Set interfaces, but not Map.

Assume the following declaration for identifiers in the table below:

Collection coll; boolean b; Object obj; int i; Iterator it;
ReturnsMethodAction
Adding objects to a collection
b = coll.add(obj) Adds obj to this collection. Returns true if the collection was changed because of the add (eg, it will always be true for adding to a List, but will be false when adding to a Set that already contains this object).
b = coll.addAll(coll) Adds all elements of obj to this collection. Returns true if the collection was changed because of the addition.
Removing objects from a collection
 coll.clear() Removes all elements from the collection.
b = coll.remove(obj) Removes one occurrence of obj from this collection. Returns true if the collection was changed because of this operation.
b = coll.removeAll(coll) Removes all elements of coll from this collection. Returns true if the collection was changed because of this operation.
b = coll.retainAll(coll) Removes all elements of which are not in coll from this collection. Returns true if th