May 21, 2022

Java Collections Framework Tutorial

Java Collections framework is an important API in Java programming language. In any Java application if you have to store objects you will certainly use one of the data structure defined in the Java collections. This Java collections tutorial gives an overview of Java collections framework; interfaces and classes that constitute the collections framework and the Java collections hierarchy.

What is Java Collections framework

A collection can be defined as a container that can store multiple elements into a single container. Collections framework in Java provides a unified architecture for defining such container classes that can store, retrieve and manipulate group of elements.

Java Collections framework contains the following-

  1. Interfaces- Interfaces in the Java collections are the abstract data types that represent collections. These interfaces provide the generalized structure for the collection which are then implemented to provide specialized implementations. For example java.util.Collection is the root interface in the collection hierarchy defining methods for the collections and the implementations of this interface like ArrayList or HashSet provide the specialized data structures.
  2. Implementations- Implementation classes are the concrete implementations of the collection interfaces. These are the reusable data structures which you can use as per your need. For example, if you want to store elements that can be accessed using an index then you can use ArrayList, if you want to ensure that only unique elements are stored then you can use HashSet.
  3. Algorithms- These are the methods that perform useful computations on the collection classes. These methods are designed in such a way that same method can be used on different collection classes. So, these algorithms provide some common functionalities that are applicable to all the collections like searching, sorting, comparing elements. Algorithms are defined as static methods in java.util.Collections class.

Interfaces in Java Collections

With in the Java Collections framework there are several core interfaces that are the foundation of the Java Collections Framework. These interfaces are designed in an evolving style, starting from more generalized to specialized interfaces. The following list describes the core collection interfaces-

  1. Collection interface- This interface is the root of the collection hierarchy which is implemented by all the collections. Though it is not directly implemented by any class, Collection interface is extended by more specific sub-interfaces like List and Set which in turn are implemented by classes.
  2. List interface- Extends Collection interface and provide behavior for an ordered collection (in which elements are stored in sequence) which can contain duplicate elements. Apart from inheriting methods of Collection interface, the List interface includes operations so that elements can be accessed using index (get, set, add, remove methods), elements can be searched returning their index (indexOf, lastIndexOf methods).
  3. Set interface- Extends Collection interface and provides behavior for a collection that cannot contain duplicate elements.
  4. Queue interface- Extends Collection interface and provides behavior for a collection where head of the queue is the element removed by a call to remove or poll.
  5. SortedSet interface- Extends Set interface and provides behavior for a sorted set. The elements in the Set are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time.
  6. NavigableSet interface- Extends SortedSet and adds navigation methods reporting closest matches for given search targets. Methods lower, floor, ceiling, and higher return elements respectively less than, less than or equal, greater than or equal, and greater than a given element, returning null if there is no such element.
  7. Deque interface- Extends Queue and provides support for element insertion and removal at both ends.

Map interfaces

  1. Map interface- Map interface provides behavior for a collection that store (key, value) pair. Note that though Map is part of Java Collections framework but it doesn’t extend the Collection interface. You can’t directly iterate a Map too, in order to iterate a Map you will have to get a Collection view of a Map and then iterate it.
  2. SortedMap interface- Extends Map interface and provides behavior for a map sorted on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time.
  3. NavigableMap interface- Extends SortedMap and adds navigation methods reporting closest matches for given search targets. Methods lowerEntry, floorEntry, ceilingEntry, and higherEntry return Map.Entry objects associated with keys respectively less than, less than or equal, greater than or equal, and greater than a given key, returning null if there is no such key.

Interfaces for iterating a collection

  1. Iterable interface- Implementing java.lang.Iterable interface allows an object to be the target of the "for-each loop" statement. Collection interface extends this interface so that collection classes can be iterated using for-each loop.
  2. Iterator interface- java.util.Iterator enables you to traverse a collection. It also allows the caller to remove elements from the underlying collection during the iteration.
  3. ListIterator interface- Extends Iterator and provides specialized behavior to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.

Java Collections classes

We have already gone through the core interface of the Java Collections framework now let’s go through the classes that implements these interfaces. Again classes evolve from general to more specific, so there are abstract classes that implement the interfaces to provide general implementation then there are more specific classes for specific collections.

  1. AbstractCollection- This abstract class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.
  2. AbstractList- This abstract class extends AbstractCollection and implements List interface in order to minimize the effort required to implement this interface.
  3. AbstractSet - This abstract class extends AbstractCollection and implements Set interface in order to minimize the effort required to implement this interface.
  4. AbstractQueue- This abstract class extends AbstractCollection and implements Queue interface to provide skeletal implementations of some Queue operations.
  5. AbstractSequentialList- Extends AbstractList to provide implementation for collection that uses sequential access (like linked list) rather than random access (like array list) of its elements.
  6. ArrayList- Java ArrayList extends AbstractList and provides resizable-array implementation of the List interface. Refer ArrayList in Java to know more about Arraylist in Java.
  7. LinkedList- Extends AbstractSequentialList and provides doubly-linked list implementation of the List and Deque interfaces.
  8. HashSet- Extends AbstractSet and provides implementation for an unordered collection that doesn't allow duplicates. Refer HashSet in Java to know more about HashSet in Java.
  9. LinkedHashSet- Extends HashSet and provides specialized implementation for a Set that maintains the iteration ordering, which is the order in which elements were inserted into the set. Refer LinkedHashSet in Java to know more about LinkedHashSet in Java.
  10. TreeSet- Extends AbstractSet and implements NavigableSet interface to provide an ordered Set. Refer TreeSet in Java to know more about TreeSet in Java.
  11. EnumSet- Extends AbstractSet and provides specialized Set implementation for use with enum types.
  12. ArrayDeque- Extends AbstractCollection and implements Deque interface to provide a Resizable-array implementation of the Deque interface. In ArrayDeque you can add and remove elements at both end points.

Map Related classes

  1. AbstractMap- This abstract class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.
  2. HashMap- Extends AbstractMap and provides Hash table based implementation of the Map interface. Refer HashMap in Java to know more about HashMap in Java.
  3. LinkedHashMap - Extends HashMap and provides specialized implementation for a Map that maintains the iteration ordering, which is normally the order in which keys were inserted into the map.  Refer LinkedHashMap in Java to know more about LinkedHashMap in Java.
  4. TreeMap - Extends AbstractMap and implements NavigableMap to provide an ordered Map. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Refer TreeMap in Java to know more about TreeMap in Java.
  5. IdentityHashMap- Extends AbstractMap and provides implementation where reference-equality is used in place of object-equality when comparing keys and values. In an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2) where as in normal Map implementations two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).
  6. EnumMap- Extends AbstractMap and provides specialized Map implementation for use with enum type keys.
  7. WeakHashMap- Extends AbstractMap and provides Hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use.

Java Collection Hierarchy

Here is a diagram depicting Java Collections framework hierarchy.
Java Collections framework

That's all for the topic Java Collections Framework Tutorial. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment