Java Collections Core Interfaces

Diagram built in BlueJ

The core collection interfaces encapsulate different collections such as List, Set, Queue and Deque. Collections operate on generics.

Collection Interface declaration :

            public interface Collection<E> extends Iterable<E>

<E> says that collection can contain any type of Object. Object can be Integer, String or any custom type Object like Customer.

Let us see all Core collection interfaces:

Collection Interface

Collection interface is root interface of all the collection interfaces. This interface acts as base for all other interfaces in collections and also Java does not provide any kind of concrete implementation of this interface. But there exists concrete implementation of interfaces that extend Collection interface i.e. Set, List, etc.

Set Interface

Set interface represents the mathematical model of set abstraction and represents sets. It enforces the rule that any implementation of set interface MUST NOT have duplicates. Common class used is HashSet.

SortedSet Interface

SortedSet interface extends Set interface hence inheriting all the methods of Set interface. SortedSet is a Set that maintains its elements in ascending order. It has several additional operations as compared to Set interface. Popular implementation of SortedSet interface is TreeSet class.

List Interface

List interface is an ordered collection. Ordered Collection means that sequence in which objects are inserted is maintained. Hence, we can leverage the index and get the element at index. Common class used is ArrayList.

Queue Interface

Queue interface is used to store elements prior to processing. Queue elements are not necessarily in First-In-First-Out order. The order is defined by respective implementations of concrete implementations of Queue Interface. For example LinkedList implements Queue and it provides methods of Queue interface to insert in FIFO order. Contrary to that PriorityQueue inserts elements in queue based in Comparator or elements based on natural ordering.

Deque Interface

Deque interface does not extend the Collection interface directly. It extends Queue interface. Apart from Collection and Queue interface functionality, it provides Last-In-First-Out. It also provided addition, deletion and retrieval of elements from both ends.

Map Interface

Map interface is used to map keys to values. Map is no way related to Collection interface. In broader terms of collection i.e. a container of objects we can say that Map is collection but it is not related to Collection interface. Map deals with key and values. A Map cannot contain duplicate keys and one key can have only one value. Popular implementation of Map interface is HashMap class.

SortedMap Interface

SortedMap interface extends Map interface hence inheriting all the methods of Map interface. SortedMap is a Map that maintains its elements in Comparable natural order. It has several additional operations as compared to Map interface. Popular implementation of SortedMap interface is TreeMap class.

Leave a Reply

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