Collection interface

In this article, we will discuss Collection interface with all its important methods in detail

 

Collection interface:

  • To represent a group of element/objects as a single unit/entity, then programmer should consider Collection
  • Collection interface is considered as the root interface of collection framework hierarchy
  • There is no concrete class which implements collection interface
  • Collection interface defines various common methods for
    1. Adding object to the collection
    2. Removing an object from collection
    3. Checking size of the collection
    4. Provide Iterator to iterate over collection
    5. Doing bulk operation
  • These methods can be used to operate on any collection objects
  • Present in java.util package and extends java.lang.Iterable interface

2-Collection-interace-in-java

Source: Team BenchResources.Net

 

Note: there is no direct concrete implementation class for Collection interface (available directly)

 

Collection interface methods:

Collection methods  Description
boolean add(Object obj); add/inserts a single element/object to invoking collection
boolean addAll(Collection c); add/inserts group of element/objects to invoking collection

(i.e.; adding specified collection to invoking collection)

 

boolean remove(Object obj);

remove/deletes a single element/object from invoking collection
boolean removeAll(Collection c); remove/deletes group of element/objects to the invoking collection

(i.e.; removing specified collection from invoking collection)

boolean retainAll(Collection c); remove/deletes all element/objects of invoking collection except specified collection

(i.e.; retaining specified collection and removing other objects from invoking collection)

void clear(); remove/deletes all element/objects from invoking collection
boolean contains(Object obj); used to search specified object from invoking collection

returns true, if present; otherwise return false

boolean containsAll(Collection c); used to search specified collection from invoking collection

returns true, if present; otherwise return false

boolean isEmpty(); checks whether invoking collection is empty or NOT

returns true, if empty; otherwise return false

int size(); returns number of elements present in the invoking collection (at the time of invoking)
Object toArray(); converts invoking collection into array

returns object array

Iterator iterator(); returns iterator for the invoking collection; to iterate over the collection elements in FORWARD direction
boolean equals(Object element); compares specified object with invoking collection for equality
int hashCode(); returns hash code for invoking collection

 

All above listed collection methods will be inherited to subsequent interfaces and classes

 

Below listed interfaces extends Collection interface

  • List
  • Set
  • Queue

 

java.util.List

  • ArrayList
  • LinkedList
  • Vector
  • Stack

 

java.util.Set

  • HashSet
  • LinkedHashSet
  • SortedSet
  • NavigableSet
  • TreeSet

 

java.util.Queue

  • PriorityQueue
  • BlockingQueue
  • PriorityBlockingQueue
  • LinkedBlockingQueue

 

Every collection classes implements java.lang.Cloneable and java.io Serializable. ArrayList and Vector in addition to Cloneable and Serializable, implements java.util.RandomAccess

Read below points to understand why it implements these marker interfaces

Marker interface Description
Random Access to access any random element/objects with same speed
Cloneable to create a duplicate object or to clone an object
Serializable to transfer objects across network

 

Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html

 

Happy Coding !!
Happy Learning !!