Things to know about - Java Collections Framework

java dev.to

Collections is a framework

while heard the term framework sounds complicate

let's simplyfy that

The Java Collections Framework is called a framework because it provides a unified architecture consisting of interfaces, implementations, and algorithms for storing and manipulating groups of objects in a standardized and reusable way.

For example, in Java, the Java Collections Framework (JCF) is a framework because it includes:

  • Interfaces that define common behavior (such as Collection, List, Set, Queue, Map).
  • Implementations of those interfaces (such as ArrayList, LinkedList, HashSet, TreeSet, HashMap).
  • Algorithms that work with collections (such as sorting, searching, shuffling, and reversing through the Collections utility class).

It is called a framework because it:

  • Provides a ready-made structure for handling collections of objects.
  • Promotes code reuse by offering common interfaces and implementations.

Before learn about collections framework

know about

what problem is faced before the collection and

problem solved by collections framework

let's talk about that


Problem before collections framework

Let's take a Array

Array is fixed size

public class CollectionDemo {
    public static void main(String[] args) {
        String[] arr=new String[5];
        arr[6]="hello";

    }
}
Enter fullscreen mode Exit fullscreen mode

here we are trying

arr string array length is 5

but we are add the element at the index of 6

but it will throw an exception

we all know that

this is the one of the problem before the collection

Another problem

public class CollectionDemo {
    public static void main(String[] args) {
        String[] arr= {"product1","product2","product3",4};


    }
}
Enter fullscreen mode Exit fullscreen mode

here

the code will throw an error

type mismatch

Only the homogenious elements allowed

This is the another problem we are facing


To solve these problems

collections framework exist

The Java Collections Framework hierarchy shows how the main interfaces and classes are related.

                     Iterable
                         │
                    Collection (Interface)
          ┌──────────────┼──────────────┐
          │              │              │
         List           Set           Queue
          │              │              │
   ┌──────┴──────┐   ┌───┴────────┐   ┌──┴────────────┐
   │             │   │            │   │               │
ArrayList   LinkedList HashSet  SortedSet        PriorityQueue
   │             │        │          │
 Vector          │     LinkedHashSet NavigableSet
   │             │        │          │
 Stack           │      TreeSet      │
                 │                   │
             (also implements Queue) │
                                     │

                    Map (Separate Interface)
                           │
           ┌───────────────┼────────────────┐
           │               │                │
        HashMap      LinkedHashMap      SortedMap
                                               │
                                         NavigableMap
                                               │
                                           TreeMap
Enter fullscreen mode Exit fullscreen mode

Main Interfaces

Interface Description Allows Duplicates Maintains Order
Collection Root interface Depends Depends
List Ordered collection Yes Yes
Set Unique elements No Depends
Queue FIFO processing Yes FIFO
Map Stores key-value pairs Keys: No Depends

Note: Map is not a child of Collection. It is a separate interface in the Java Collections Framework.

Common Implementations

List

  • ArrayList – Dynamic array, fast random access.
  • LinkedList – Doubly linked list, efficient insertions and deletions.
  • Vector – Synchronized version of ArrayList.
  • Stack – LIFO stack, extends Vector.

Set

  • HashSet – Unordered, unique elements.
  • LinkedHashSet – Maintains insertion order.
  • TreeSet – Stores elements in sorted order.

Queue

  • PriorityQueue – Elements ordered by priority.
  • LinkedList – Can also be used as a queue.

Map

  • HashMap – Unordered key-value pairs.
  • LinkedHashMap – Maintains insertion order.
  • TreeMap – Keys are stored in sorted order.
Collection
├── List  → Ordered, Duplicates Allowed
├── Set   → Unique Elements
└── Queue → FIFO

Map → Key-Value Pairs (Separate from Collection)
Enter fullscreen mode Exit fullscreen mode

Source: dev.to

arrow_back Back to Tutorials