Collection Interface

collection interface

collection interface implementing classes and methods

A collection is an object which represents a group of objects as it’s elements. To manipulate these collections(objects) the Collection interface provides some basic operations like add() , remove() methods and also sub-interfaces like Set and List and so on. Collection interface does not provide any direct implementation classes like other sub interfaces  Set , List etc , but it provides core methods for basic operations over collections.  Java Collection interface is the foundation for the collections framework but the super interface is Iterable. Sub-interfaces like Set , List are some kind of collections , List is an ordered collection but Set is not an ordered collection (will be discussed in coming chapters). 

Java Collection Interface

Before knowing the behavior of the collection object , let’s take a look how actually a collection object will be . In order to create a collection object we need to make use of two standard constructors respective of their implementation classes : a void ( no argument constructor) which creates an empty collection and a constructor with single argument of type collection , which creates a new collection with the same elements present in the collection specified as argument.

Collection Interface

  • Collection interface contains methods that perform basic operations like add(Object O) for adding the specified  element into the collection , remove(Object O) method to remove the specified element from the collection and also methods like int size() to know the capacity of the collection.We will discuss all core methods down the line.
  • Collection interface does not provide any methods for retrieving the elements from the collection object because , Collection interface does not have any direct implementation classes. To retrieve elements from a collection , we need to make use of the sub-interfaces direct implementation classes.
  • Collections that do not support modification operations such as ‘add’ , ‘remove’ and ‘clear’ are referred as unmodified collections , collections that support modification operations are called modified collections.

Collection Interface Methods 

collection interface unsynchronized

 Index  Method
 1 add(Object O)

public boolean add(Object O)

  • This method adds the specified element into the collection.
  • Returns “true” if the element is added successfully.
  • Returns “false” if the collection does not allow duplicate elements or the element is already existed.
    • Some collections does not allow to add “null” and some kind of collections have some restrictions on the type of element adding.

Exceptions

Throws

  1. UnsupportedOperationException – if the add operation is not supported by this collection
  2. ClassCastException : if the class of the specified element prevents it from being added to the collection
  3. NullPointerException : If the specified element is “null” and the collection does not allow to add null elements
  4. IllegalArgumentException : Suppose the specified element is ‘String’ type but the collection allows only ‘int’ type elements , then this exception occurs
  5. IllegalStateException : If the element cannot be added at this time due to it’s insertion restrictions
 2 addAll(Collection c)

public boolean addAll(Collection c)

  • ‘addAll’ method comes under optional operation. 
  • It adds all the elements present in the specified collection.
  • ‘c’ is the collection to be added.
  • Return  ‘true’ if the specified collection is added successfully or else returns ‘false’ if the specified collection contains any null elements or some of the elements are already existed(if the collection does not allow null elements and duplicate elements to be added).

Exceptions 

Throws

  1. UnsupportedOperationException – if the addAll operation is not supported by this collection
  2. ClassCastException : if the class of the specified element prevents it from being added to the current collection
  3. NullPointerException : If the specified collection contains  a “null” element and the collection does not allow to add null elements , or if the specified collection is null.
  4. IllegalArgumentException : Suppose the specified collection is ‘String’ type but the collection allows only ‘int’ type elements , then this exception occurs
  5. IllegalStateException : If not all the elements can be added at this time due to it’s insertion restrictions
 3 remove(Object O)

public boolean remove(Object O)

  • ‘remove’ is an optional operator.
  • It removes the specified element in the collection (if it is present) 
  • Returns true if the operation is successful or else returns false if the specified element is not present in the collection to be removed.

Exceptions

Throws

  1. UnsupportedOperationException – if the remove operation is not supported by this collection
  2. ClassCastException : if the type of the specified element is incompatible with the current collection.
  3. NullPointerException : If the specified element contains  a “null” element and the collection does not allow null elements.
 4 removeAll(Collection c)

public boolean removeAll(Collection c)

  • Removes all the elements specified in the collection if they are present in the current collection. 
  • This method is an optional operation.
  • After successful operation , the present collection does not contain any elements which are present in the specified collection ‘c’.
  • ‘c’ –> Collection containing elements to be removed.
  • Returns true if the operation is successful or else returns false if the operation is unsuccessful.

Exceptions

Throws

  1. UnsupportedOperationException – if the removeAll operation is not supported by this collection.
  2. ClassCastException : if the type of the specified collection is incompatible with the current collection.
  3. NullPointerException : If the current collection contains one or more null elements and the specified collection does not support null elements  (OR) if the specified collection is null then this exception will occur.
 5 retainAll(Collection c)

public boolean retainAll(Collection c)

  • Removes all the elements from the current collection , except the elements present in the specified  collection ‘c’.
  • Returns true if the operation is successful or else returns false if the operation fails to remove.
  • retainAll is an optional operation.

Exceptions

Throws

  1. UnsupportedOperationException – if the retainAll operation is not supported by this collection.
  2. ClassCastException : if the type of the specified collection is incompatible with the current collection.
  3. NullPointerException : If the current collection contains one or more null elements and the specified collection does not support null elements  (OR) if the specified collection is null then this exception will occur.
 6 clear()

public void clear()

  • clear() method is an optional operation.
  • It removes all the elements in the current collection and the collection will be empty after the operation is successful.

Exception

Throws

  1. UnsupportedOperationException – if the clear operation is not supported by this collection.
 7 isEmpty()

public boolean isEmpty()

  • Checks if the current collection is empty or not.
  • Returns true if the collection is empty , or else returns false if the collection contains any elements.
 8 size()

public int size()

  • Returns the number of elements present in the current collection.
  • If the current collection contains more than INTEGER.MAX_VALUE then it returns INTEGER.MAX_VALUE.
  • This method does not throw any exceptions and this is not an optional operation.
 9 contains(Object element)

public boolean contain(Object element)

  • This method is used for searching only for one element in the current collection. 
  • Returns ‘true’ if the specified element is present at least once in the current collection. or else returns false if the specified element is not present in the current collection.
  • This method is not an optional operation.

Exceptions

Throws

  1. ClassCastException : If the type of the specified element is incompatible with the current collection.
  2. NullPointerException : If the specified element is null and the current collection does not allow null elements then this exception will occur.

 10 containsAll(Collection c)

public boolean containsAll(Collection c)

  • This method is generally used for searching a group of elements in the current collection.
  • Returns true if the current collection is containing all of the elements present in the specified collection ‘c’. or else returns false if at least one element is not present in the current collection.
  • ‘c’ –> specified collection

Exceptions

Throws

  1. ClassCastException : If the types of one or more elements in the  specified collection is incompatible with the current collection.
  2. NullPointerException : If the specified collection contains   null elements  and the current collection does not allow null elements then this exception will occur. (OR) this exception will occur if the specified collection is null.

 11 toArray()

public Object[] toArray()

  • This method is used to copy all of the elements present in the current collection to a new array.
  • In other words it returns a new array and the array contains the elements present in the current collection.
  • While creating a new array , it takes the elements in the current collection one by one by using the iterator operation. 
  • Iterator is used traverse the elements from a collection.
  • After returning all the elements to a new array , the programmer can do any modifications to the new array i.e the returned array is “safe”.
  • This method is known as a bridge between array-based and collection-based API’s.

 12 iterator()

public Iterator iterator()

  • This method is used to get the elements from a collection. In others words iterator() returns the elements present in the collection one by one.
  • ‘Iterator’ is an interface , so the return type is Iterator.
  • While iterating the elements from the collection , there is no guarantees on which order the elements are returning.
  • For example if there is a collection ‘c’ which contains int type of elements , then
Iterator itr = c.iterator();

while(itr.hasNext())

{

foo(i.next());

}
  • This is how an iterator works over a collection of elements, hasNext() and next() are the methods belong to iterator interface.
  • hasNext() method checks whether the next element is present or not , if the next element is present then it enters the loop and next() method will return that element. 

 13 equals(Object O

public boolean equals(Object O)

  • This method is used to compare two objects (it can be a collection too)
  • This method is applicable only to the classes which implements the Collection interface directly.
  • If the programmers class is implementing List or Set interface then this method will not work  in other words if you want to override this method then your class must implements to Collection interface directly.
  • The general rule for the object.equals method states that the comparison must be symmetric i.e
    • a.equals(b) should be true if and only if b.equals(a).
  • Returns true if the specified object is equal to the current collection.

2 thoughts on “Collection Interface

  1. I’m truly enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Excellent work!

Leave a Reply

Your email address will not be published. Required fields are marked *