DEV Community

Harini
Harini

Posted on

Collections in Java

What is Collections Framework in Java?

The Java Collections Framework (JCF) is a set of classes and interfaces provided by Java to store, manage, and manipulate groups of objects efficiently.

Before Collections, developers mainly used arrays. Arrays have a fixed size and limited functionality. Collections provide dynamic storage and many built-in methods for handling data.

Package: java.util

Why is Collections Used?

Collections are used to:

  • Store multiple objects dynamically.
  • Perform searching, sorting, insertion, and deletion easily.
  • Reduce coding effort through built-in methods.
  • Improve code reusability and maintainability.
  • Handle large amounts of data efficiently.

Without Collections

int[] arr = new int[100];
Enter fullscreen mode Exit fullscreen mode
  • Fixed size
  • Difficult to resize
  • Limited operations

With Collections

ArrayList<Integer> list = new ArrayList<>();
Enter fullscreen mode Exit fullscreen mode
  • Dynamic size
  • Easy insertion and deletion
  • Rich utility methods

Advantages

  • Dynamic Size
  • Ready-Made Data Structures
  • Better Performance
  • Reusability and Maintainability

List Interface

  • A List stores elements in insertion order and allows duplicates.

Features

  • Ordered
  • Duplicate elements allowed
  • Index-based access

Set Interface

  • A Set stores unique elements only.

Features

  • No duplicates
  • Usually unordered
  • Faster searching

Map Interface

  • Map stores data as Key-Value pairs.
  • A key must be unique.

Features

  • Key-Value storage
  • Duplicate keys not allowed
  • Values can be duplicated

Top comments (0)