Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

The java.util Package - The Collection Interface

  • this is the root interface for the collection heirarchy
  • it is not directly implemented by an SDK class; instead they implement the subinterfaces List or Set
  • it is typically used to manipulate and pass collections around in a generic manner
  • classes which implement Collection or one of it's subinterfaces must provide two constructors
    1. a default, no-argument constructor, which creates an empty collection, and
    2. a constructor which takes a Collection as an argument and creates a new collection with the same elements as the specified collection
Query Methods
contains(Object o) returns true if the collection contains the specified element
isEmpty() returns true if the collection has no elements
iterator() returns an Iterator object.
There is no guarantee as to the order of the returned elements unless the collection is an instance of a class that guarantees the order.
size() returns the number of elements in the collection or Integer.MAX_VALUE if the collection equals or exceeds Integer.MAX_VALUE
toArray() returns the collection elements as an array.
If the collection class guarantees an order, the array elements are in the guaranteed order.
toArray(Object a[]) returns all the elements in the collection whose type is that of the array type.

If the collection does not fit in the array, a new array of the same type is returned.

If the array is larger than the collection, the array element after the last collection element is set to null
Bulk Methods
containsAll(Collection c) returns true if the collection contains the all the elements in the specified collection
addAll(Collection c) adds all the elements in the specified collection to this collection
clear() removes all the elements in the collection
removeAll(Collection c) removes all the this collections elements that are in the specified collection.
retainAll(Collection c) retains all the elements in this collection that are contained in the specified collection
Modification Methods
add(Object o) adds an element to the collection.
Returns false if the element is not added as the collection class guarantees no duplicates.
remove(Object o) removes the specified object from the collection, if it exists.
equals() and hasCode()
equals(Object o) programmers may override the Object.equals() method to implement collection specific comparisons eg "value" comparison vs "reference" comparison
hashCode() programmers overriding equals() must also override Object.hashCode()

Tips

  • any SDK class which implements Collection or any of it's subinterfaces will contain the two required constructors CollectionName() and CollectionName(Collection c)

Collections Framework Collection Abstract Classes Iterator List