Collection framework in java

What you will learn here about Collection

  • What is collection in java
  • Collection hierarchy in java
  • Collection interface in java
  • List interface in java
  • Implementation of List interface in java
  • Set interface in java
  • Implementation of Set interface in java

What is collection in java

In java Collection is framwork which has different interfaces and classes which helps to represent group of individual objects as single entity. For example, you have downloaded so many movies then you can you say you have good collection of movies. Similar way, in java Collection provides interfaces and classes for representing group of individual objects as single entity.

Collection hierarchy in java

The Collection hierarchy in java is given below
Collection hierarchy in java

Collection interface in java

Collection is the child interface of the Iterable interface. If we want to represent group of individual objects as a single entity then we should go for collection. Collection interface has most methods which are applicable for entire collection object. Non of the concreate class implements Collection interface directly.

Collection interface methods

The list of Collection interface methods is given below
collection interface methods

List interface in java

  1. List interface is the child interface of Collection interface
  2. If we want to represent group of individual object as single entity where duplicates are allowed and insertion order is preserved then we should go for list
  3. insertion order is also preserved by index
  4. List is index based
  5. we can differentiate duplicates by using index

List interface methods

The List interface methods are given below
list interface methods

Implementation of List interface in java

    Following classes provide the implementation of List interface.

  1. ArrayList
  2. LinkedList
  3. Vector

ArrayList

  • ArrayList is the implementation of the List interface
  • If we want to represent group of individual objects as a single entity where duplicates are allowed and insertion order is preserved then we should go for ArrayList
  • Underline data structure is growable or resizable. Internally ArrayList uses Object array
  • Since List is index based insertion order is preserved
  • In ArrayList we can store Homogeneous as well as Hetrogenous objects.

LinkedList

  • LinkedList is the implementation of the List interface
  • If we want to represent group of individual objects as a single entity where duplicates are allowed and insertion order is preserved then we should go for LinkedList
  • LinkedList follows doubly link list data structure internally so LinkedList is best choice if frequent operation is insertion and deletion

LinkeList specific methods

LinkeList specific methids

Vector

  • Vector is the implementation of the List interface
  • If we want to represent group of individual objects as a single entity where duplicates are allowed and insertion order is preserved then we should go for Vector
  • Underline data structure is growable or resizable.
  • Since List is index based insertion order is preserved
  • In Vector we can store Homogeneous as well as Hetrogenous objects.
  • Most of the method presents in Vector are syncronized so vector provides thread safety

Stack

  • Stack is the child class of Vector class
  • Stack specially designed for implementing Last in First Out(LIFO)

Stack specific methods

Stack specific methods

Queue interface in java

  • Queue is the child interface of Collection interface.
  • In queue elements are stored in first come first out manner.
  • If we want to represent group of individual objects as single entity for prior to processing then we should go for Queue

Queue Specific methods

Queue Specific methods

Set interface in java

  • If we want to represent group of individual object as single entity where duplicates are not allowed and insertion order is not preserved then we should go for Set
  • Its chlid interface of Collection interface
  • There are no specific methods for Set interface, so we have to use Collection methods only

Implementation of Set interface in java

    Following classes provides the implementations of the Set interface

  1. HashSet
  2. TreeSet

HashSet

  • HashSet is implementation class of Set interface
  • Underlying data structure for HashSet is Hashtable
  • Duplicates are not allowed but if we are trying add any duplicate value then we do not get any compile time error instead add() method returns false
  • Insertion order is not preserved because all objects are inserted based on Hashcode of Objects
  • Hetrogeneous objects are allowed
  • null insertion is possible
  • HashSet is best choice if operation is frequent search operation

LinkedHashSet

  • LinkedHashSet is the child class of HashSet
  • If we want to represent group of individual objects as single entity where duplicates are not allowed but insertion order is preserved then we should go for LinkedHashSet
  • Underlying data structure is Hashtable as well as double linked list

SortedSet

  • If we want to represent group of individual objects as single entity with some sorting order where duplicates are not allowed then we should go for SortedSet
  • SortedSet is the child interface of Set interface

SortedSet specific method

SortedSet specific methods

NavigableSet interface

NavigableSet is the child interface of sortedset, it has several methods which provides supports for navigation

TreeSet

  • TreeSet is the child class of NavigableSet interface
  • Underlying data structure is balance tree
  • Duplicates are not allowed
  • Insertion order is not preserved but all objects are inserted according to some sorting order
  • Only Homogeneous objects are allowed. Hetrogeneous objects are not allowed. If we try to add it gives ClassCastException

You may also like...