|
Collection interface provides us a foundation upon which the collection framework is based at and it provides us with concrete classes which can be played around to have various kinds of implementations. It gives us the freedom to play around with the group of objects.
This provides us different kind of interfaces and it is always at the top of the collections Hierarchy.
The collection framework provides with a dozen methods that very efficiently describe the common operations which we are capable on performing the groups of objects.
We have few very common interfaces like :
Collection it basically helps us to work over the groups of objects and as stated earlier in this section it sits always on the top ranking on collection hierarchy.
List Extends Collection to handle sequences (lists of objects)
Set Extends Collection to handle sets, which must contain unique elements
SortedSet Extends Set to handle sorted sets
Comparator as the name suggest defines how to objects are compared.
Iterator and ListIterator enumerate the objects within a collection.
Random Access again as per term indicates that we can have random Access to the elements of the list.
This was just a one liner definition to get you the acquaintance with the interfaces and we will have a look on each one of them in our section below:
We have two broadly categorised interfaces:
1. Modifiable Interfaces: Collections that allow its methods defined within itself to be played around or in some words to be modified are called modifiable interfaces.
2. Unmodifiable interfaces: Collections which do not provide the flexibility to its methods to be altered or changed belong to the category of unmodifiable interfaces.
A short list of few methods which are defined by collections is :
a. boolean add(Object obj) :This method Adds obj to the invoking collection and returns true if obj was added to the collection else giving if it is already a member of the collection, or if the collection does not allow duplicates.
b. boolean addAll(Collection c) : This method adds all the elements of c to the invoking collection. And gives value as true if the operation succeeded (i.e., the elements were added) else false.
c. void clear( ) : This method as per its term removes all elements from the invoking collection.
d. boolean contains(Object obj) : This returns true if obj is an element of the invoking collection else returns false.
e. boolean containsAll(Collection c) : Gives Value as true if the invoking collection contains all elements of c. else false.
f. boolean equals(Object obj) : Returns true if the invoking collection and obj are equal else returns false.
g.int hashCode( ) : This method returns the hash code for the invoking collection.
h. boolean isEmpty( ) : This method returns true if the invoking collection is empty else gives false.
i. Iterator iterator( ) :It returns an iterator for the invoking collection.
j. boolean remove(Object obj) : This method removes one instance of obj from the invoking
Collection. It will return value as true if the element is erased else returns false.
k. boolean removeAll(Collection c) : This method removes all elements of c from the invoking collection. It Returns true if the collection has been changed or the elements are removed earlier only.
l. boolean retainAll(Collection c) : This method removes all elements from the invoking collection except those in c. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.
m. int size( ) : This method returns the number of elements held in the invoking collection.
|