Skip to content

Interfaces of collection framework

  • by

9 key interfaces of collection framework:

 1. Collection Interface :

  • If we want to represent a group of individual objects as a single entity then we should go for collection.
  • The collection interface defines the most common methods which are applicable for any collection object.

For ex:-

Add Object

Remove Object

Contains()

  • In general collection interface is considered as root interface of collection framework.
collection framework

NOTE:-THERE IS NO CONCRETE CLASS THAT IMPLEMENTS THE COLLECTION INTERFACE DIRECTLY.

Difference between collection and collections

Collection

Collections

The collection

is an interface that

can be used to represent a group of individual objects as a single entity.

Collections is a utility class present in java.util.package to define several utility methods (Like sorting searching) for collection objects.

2. List Interface :

  • The list is the child interface of the collection.
  • If we want to represent a group of individual objects as a single entity where duplicates are allowed and insertion order preserved then we should go for a list.
java list collection
  • Legacy classes (vector and stack) :- Classes coming from older version/generation are called as legacy classes.

3. Set Interface :

  • It is the child interface of collection.
  • If we want to represent a group of individual objects as a single entity where duplicates are not allowed and insertion order not preserved then we should go for the set.

java set interface collection framework

Difference between list and set

LISTSET
1.Duplicates are allowed1.Duplicates are not allowed
2.Insertion order preserved2.Insertion order not presetved

4. Sorted Set Interface :

  •  It is the child interface of the set.
  •  If we want to represent a group of individual objects as a single entity where duplicates are not allowed but all objects should be inserted according to some sorting order then we should go for a sorted set.
java sorted set collection

5. Navigable Set Interface :

It is the child interface of sorted set it defines several methods for navigation purpose.

java navigable set collection

6. Queue Interface :

  • It is the child interface of collection.
  • If we want to represent a group of individual objects prior to processing then we should go for the queue.
java queue collection

For Ex:- Before sending a mail all mail id’s we have to store somewhere and in which order we saved in the same order mails should be delivered(first in first out) for this requirement queue concept is the best choice.

Note:-All the above interfaces (Collection, List, Set, SortedSet, NavigableSet, and Queue) meant for representing a group of individual objects.

If we want to represent a group of objects as key value pairs then we should go for map interface.

7. Map Interface :

  •  The map is not the child interface of collection.
  •  If we want to represent a group of individual objects as key-value pairs then we should go for Map.
java map interface

For Ex:- We have data (below). Both keys and values are objects, duplicate keys are not allowed but values can be duplicated.

ROLL NO

NAME

101

Neeraj

102

Preeti

103

Shephali

8. Sorted Map Interface :

  •  It is the child interface of Map.
  •  If we want to represent a group of key-value pairs according to some sorting order then we should go for a sorted map.
java sortedmap collection

9. Navigable Map Interface :

  •  It is the child interface of Sorted Map.
  •  It defines several utility methods for navigation purposes.

java Navigablemap collection
SORTING

  1. Comparable(I):- Default native sorting order.
  2. Comparator(I):-If we want to implement customize sorting.

CURSORS

IF YOU WANT ONE BE ONE OBJECTS FROM COLLECTIONS

  1. Enumeration(I)
  2. Iterator(I)
  3. ListIterator(I)

COLLECTION INTERFACE DETAILS

  • If we want to represent a group of individual objects as a single entity then we should go for collection.
  • In the general collection, the interface is considered as the root interface of the collection framework.
  • The collection interface defines the most common methods which are applicable for any collection objects.

Important methods of collection interface

  • boolean add(Object o)
  • Boolean addAll(Collection c)
  • boolean remove(Object o)
  • boolean removeAll(Collection c)
  • boolean retainAll(Collection c)
  • void clear()
  • boolean contains(Object o)
  • boolean containsAll(Collection c)
  • boolean isEmpty()
  • int size()
  • Object[] to Array()
  • Iterator iterator()

NOTE:- 1) Collection interface doesn’t contain any method to retrieve objects there is no concrete class which implements collection class directly.

2) No get methods.

Leave a Reply

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