CodingBison

Java Collections framework, has quite a few number of interfaces and implementation classes, all the available interfaces are designed using the standard object oriented concepts. Thus, all the generic methods/properties are defined in root interface, and the rest of interfaces inherit the generic properties and also define their own.

Collection interface is the root interface of the collection hierarchy, more specialized interfaces and their implementation classes, that are available in the Collection framework are represented in the below diagram.



Figure: Collection Frame Interface Hierarchy

The above figure represents only the interface hierarchy, a detailed diagram that contains the interfaces as well as their implementation classes is provided at the end of this page. As we go through this module, we will look deeply into each of the interfaces, their implementation classes and their available methods.

Methods defined in Collection interface

Since the interface Collection is the root of the collection hierarchy and other interfaces extends this interface, it is important that all the generic methods are defined at the root itself. Here is the list of the methods defined in the Collection Hierarchy.


Table: Generic Methods defined in the Collection Interface
MethodDescription
bool add(Object o)Adds an object "o" to the collection that invokes this method. Add operation adds the object to the end of the list. Returns True on Success, False on Failure. Note: Failure case depends on the Collection type.
bool addAll(Collection c)Adds all the objects of collection "c", to the Collection that invokes this method. addAll operation adds the collection to the end of the list. Returns True on Success, False on Failure. Note: Failure case depends on the Collection type.
bool remove(Object o)Removes an object "o" from the collection that invokes this method. Remove operation always removes the first occurrence of the object. Returns True on Success, False on Failure. Note: Failure case depends on the Collection type.
bool removeAll(Collection c)Removes all the objects of collection "c", from the Collection that invokes this method. Returns True on Success, False on Failure. Note: Failure case depends on the Collection type. For example, alistObj2.removeAll(alistObj1); "Where alistObj1 and alistObj2 are collection objects". alistObj2.removeAll(alistObj2); Please note, if the invoking object is same as invoked object, in this case alistObj2, then all the elements in the collection alistObj2 will be removed, which means empty the collection
bool clear()Remove all the objects from the collection that invokes this method. Returns True on Success, False on Failure. Note: Failure case depends on the Collection type.
bool contains(Object o)Checks if object "o" is part of the collection that invokes this method. Returns True on Success, False on Failure. Note: Failure case depends on the Collection type.
bool containsAll(Collection c)Checks if the invoking collection contains all the objects of collection "c". Returns True on Success, False on Failure. Note: Failure case depends on the Collection type.
bool retainAll(Collection c)Retains all the objects of collection "c", from the Collection that invokes this method. So, the invoking collection will contain only the objects of collection "c", rest everything will be removed. Returns True on Success, False on Failure.
bool isEmpty()Checks if the invoking collection is empty. Returns True on Success, False on Failure. Note: Failure case depends on the Collection type.
int size()Returns the size of the collection that invokes this method.
bool equals(Object o)Compares if object "o" is equal to the object that invokes this method. a.equals(o); " Where a and o are object (Could be collection objects, Set object, List object etc)". equals method just compares 2 objects and returns true if they are absolutely equal. For better understanding, please take a look at equals method descriptions List, Set interfaces.
int hashCode()Returns the hash code value. This method is used internally by the hashset and hashtable related interfaces. So, when a key is to be inserted into the hash table, hash function generates a hash code that determines where to insert the entry. This value could be same for different keys (due to collisions). Please take a look at hash tables section in the codingbison.com/data-structures-in-c module, to learn more about hash tables.
Object[ ] toArray()Returns an array of all the objects present in the collection that invokes this method.
Iterator<obj> iterator()Returns an iterator over the collection that invokes this method. The returned iterator object is used to iterate over all the objects present in the Collection. We will see more about Iterators in the later sections on this module.

Please note that the above table is not the complete list! Few of the methods defined in the Collection interface are not listed there, and we will see them as and when we use them.

Overview of the Collection Framework

As mentioned above, the Collection interface sits at the root of the Java Collections framework that provides quite a few number of interfaces and implementation classes. For the time-being, we provide the common Interfaces/Classes supported by the Collection in the picture below. Don't be overwhelmed by the picture below!! As we go through this module, we would cover these interfaces and classes, one by one and also write tons of sample programs to help us understand them better.



Figure: Collection Framework Interface Hierarchy





comments powered by Disqus