Collections Hierarchy Java

Collections Hierarchy Java

After knowing the meaning and purpose of Collections framework, let us know the basic interfaces, derived classes and their properties. Java data structures are nothing but subclasses of Collection and Map interfaces. Following hierarchy gives all the interfaces of Collections framework.

Collections Hierarchy

Fig: Basic interfaces of collections framework

Map and SortedMap form a separate hierarchy and not connected with Collection interface. Bu still, they are treated as part of collections framework. It is done so to get interoperability with collections and legacy classes. Observe, interfaces Map and SortedMap are not derived from Collection interface. Collection being at the top of the hierarchy, it gives generic methods for all the derived classes.

Note: Framework name is collections and the interface name is Collection. Do not get confused; later, we get one more class called Collections.

Description of fundamental interfaces of Collections Hierarchy

A table is given showing the core interfaces with their implemented classes with brief description.

Interface Brief Description Derived Classes
Collection It forms the root interface and comes with methods that define basic operations and makes all DS as one unit (called by a common name) Majority of all DS come under this interface
Set Subclasses of this interface have a common feature of not allowing duplicate elements HashSet, LinkedHashSet
SortedSet Subclasses of this interface come with a common feature of printing the elements in ascending order implicitly. Being sub interface of Set, it allows only unique elements TreeSet
List Subclasses allow duplicate elements LinkedList, ArrayList, Vector
Map Allows the subclasses to store key/value pairs. Does not allow duplicate keys. HashMap, LinkedHashMap, Hashtable
SortedMap Prints elements in ascending order of keys. Being a sub interface of Map, it inherits the property of not allowing the duplicate keys. TreeMap

Table: Description of Main interfaces of Collections Hierarchy

The elements of List can be accessed with their index numbers.

Collection classes store elements of single entities, but Map stores key/value pairs; to store a value, a key must be supplied along. SortedSet and SortedMap print their stored elements in ascending order (known as natural order) by default. The order can be customized explicitly using Comparator interface or Comparable interface.

Following figure displays the main interfaces with relation to their derived classes.

As it can be observed, there comes some abstract classes in between interfaces and concrete classes (DS classes). They add some extra features to subclasses other than inherited from interfaces.

Following table gives the properties of derived classes.

Data Structure Interface Duplicates Methods available DS that inherits
HashSet Set Unique elements equals(), hashCode() Hashtable
LinkedHashSet Set Unique elements equals(), hashCode() Hashtable, doubly-linked list
TreeSet SortedSet Unique elements equals(), compareTo() Balanced Tree
ArrayList List Duplicates allowed equals() Resizable array
LinkedList List Duplicates allowed equals() Linked list
Vector List Duplicates allowed equals() Resizable array
HashMap Map Unique keys equals() and hashCode() Hash table
LinkedHashMap Map Unique keys equals() and hashCode() Hash table and doubly-linked list
Hashtable Map Unique keys equals(), hashCode() Hash table
TreeMap SortedMap Unique keys equals(), compareTo() Tree Map

Figure: Collections Features

There are two distinct hierarchies monitored by Collection and Map. Map is also placed in collections framework to have interoperability.

Basic Features of Main Interfaces of Collections Hierarchy

Core collection interfaces are the base to Java Collections framework. They include the interfaces Collection, Set, List, Queue and Map on which all the data structures are built.

  1. Collection is the root interface for all the hierarchy (except Map).
  2. Set interface unique feature is that it does not accept duplicate elements. That is, no two elements will be the same.
  3. SortedSet interface is derived from Set interface and adds one more feature that the elements are arranged in sorted order by default.
  4. List interface permits duplicate elements.
  5. Queue interface holds elements and returns in FIFO order.
  6. Map adds the elements in key/value pairs. Duplicate keys are not allowed, but duplicate values are allowed. One key can map one value only.
  7. SortedMap interface is a particular case of Map. Keys are sorted by default.

19 thoughts on “Collections Hierarchy Java

  1. Narendra Reddy

    If the ListIterator Interface implements the Iterator Interface, It should Implement the basic methods of the List Interface right !
    Then why ListIterator Interface is not used to iterate through the set??

  2. Sunita Bansal .

    Thanks for giving great explanation. I have two questions
    where the iterator and list iterator come into the above hierarchy?
    where is the comparator and comparable interface in the above hierarchy?

  3. Deepak Sharma

    Can you please explain me why the listIterator() method is put in List Interface but not the Collection interface?If the same method had been put in the Collection interface then both the Set and List could be use it.

      1. Deepak Sharma

        Sir,I know that Set is not permitted to use ListIterator interface.But why question is why?why only List has permission to use ListIterator interface not the Set..

  4. Rakesh

    Collections “Interface->abstract class->concrete class” hierarchy is great.
    I would request you to please also post the Map (Interface->abstract class–>concrete class) hierarchy. It would be really nice that way to learn map hierarchy as well.

  5. santosh

    Q>what is the interface name in collection framework which does not allow duplicate value and follow natural sorting order?
    option are:

Leave a Reply

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