Java Collections Framework in Depth with Examples for Beginners

The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion, etc. can be achieved by Java Collections.
Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers).

What Is a Collections Framework?

A collections framework is a unified architecture for representing and manipulating collections.
All collections frameworks contain the following:
  • Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
  • Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
  • Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.

Benefits of the Java Collections Framework

The Java Collections Framework provides the following benefits:
  1. Reduces programming effort by providing data structures and algorithms so you don't have to write them yourself.
  2. Increases performance by providing high-performance implementations of data structures and algorithms. Because the various implementations of each interface are interchangeable, programs can be tuned by switching implementations.
  3. Provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth.
  4. Reduces the effort required to learn APIs by requiring you to learn multiple ad hoc collection APIs.
  5. Reduces the effort required to design and implement APIs by not requiring you to produce ad hoc collections APIs.
  6. Fosters software reuse by providing a standard interface for collections and algorithms with which to manipulate them.

Hierarchy of Collection Framework

Let us see the hierarchy of Collection framework. The java.util package contains all the classes and interfaces for Collection framework.

The collection interfaces are divided into two groups:

The most basic interface, java.util.Collection.
 The other collection interfaces are based on java.util.Map and are not true collections

Collection Interfaces

The collection interfaces are divided into two groups. The most basic interface, java.util.Collection, has the following descendants:
  • java.util.Collection 
  • java.util.List
  • java.util.Set 
  • java.util.SortedSet 
  • java.util.NavigableSet 
  • java.util.Queue 
  • java.util.concurrent.BlockingQueue 
  • java.util.concurrent.TransferQueue 
  • java.util.Deque 
  • java.util.concurrent.BlockingDeque 
The other collection interfaces are based on java.util.Map and are not true collections. However, these interfaces contain collection-view operations, which enable them to be manipulated as collections. Map has the following offspring:
  • java.util.Map
  • java.util.SortedMap
  • java.util.NavigableMap 
  • java.util.concurrent.ConcurrentMap 
  • java.util.concurrent.ConcurrentNavigableMap

Collection Implementations

Classes that implement the collection interfaces typically have names in the form of . The general purpose implementations are summarized in the following table:
The AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList and AbstractMapclasses provide basic implementations of the core collection interfaces, to minimize the effort required to implement them. The API documentation for these classes describes precisely how each method is implemented so the implementer knows which methods must be overridden, given the performance of the basic operations of a specific implementation.

Commonly used implementations

The Java Collections Framework provides several general-purpose implementations of the core interfaces:
  • For the Set interface, HashSet is the most commonly used implementation.
  • For the List interface, ArrayList is the most commonly used implementation.
  • For the Map interface, HashMap is the most commonly used implementation.
  • For the Queue interface, LinkedList is the most commonly used implementation.
  • For the Deque interface, ArrayDeque is the most commonly used implementation.

Java Collections Tutorials/Guides


List Implementations

General-Purpose List Implementations:
General-Purpose Set Implementations:
Special-Purpose Set Implementations:

Map Implementations

General-purpose Map Implementations:

Aggregate Operations(Streams)

Collections Helper/Utility Classes

          - Sorting
          - Shuffling
          - Searching
          - Composition
          - Finding Extreme Values

Convert Collections Examples

Best Practices and Examples

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.