Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 24

Collections Framework in Java

In this lesson: Java Collections Framework explained — ArrayList, LinkedList, HashSet, HashMap — when to use which, with code. In Hindi & English.

What is the Collections Framework

The Collections Framework is a set of ready-made classes for storing groups of objects. Instead of building your own data structures, you use proven ones like ArrayList, HashSet and HashMap. They are grouped under three main ideas: List, Set and Map.

💡 List = ordered, allows duplicates. Set = unique only. Map = key-value pairs.

Why use it over arrays

  • Grows automatically — no fixed size like an array.
  • Built-in operations — add, remove, search and sort with ready methods.
  • Many structures — pick a List, Set or Map to fit the job.

Arrays are fine for fixed-size data, but collections are far more flexible for real programs.

List

import java.util.ArrayList;

ArrayList<String> names = new ArrayList<>();
names.add("Aman");
names.add("Diya");
names.add("Aman");        // duplicates allowed
System.out.println(names);
[Aman, Diya, Aman]

A List keeps items in order and allows duplicates. ArrayList is the most common List — it grows as you add, and you access items by index.

Set

import java.util.HashSet;

HashSet<String> ids = new HashSet<>();
ids.add("A1");
ids.add("A2");
ids.add("A1");            // duplicate ignored
System.out.println(ids.size());
2

A Set stores only unique items — duplicates are dropped automatically. HashSet is the common choice, great for removing duplicates or checking membership fast.

Map

import java.util.HashMap;

HashMap<String, Integer> marks = new HashMap<>();
marks.put("Aman", 88);
marks.put("Diya", 92);
System.out.println(marks.get("Diya"));
92

A Map stores key-value pairs, so you look up a value by its key. HashMap is the common choice, giving fast lookup — perfect for name-to-marks or id-to-object.

Choosing the right one

NeedUse
Ordered list, duplicates OKArrayList (List)
Only unique valuesHashSet (Set)
Look up by a keyHashMap (Map)
Sorted orderTreeSet / TreeMap

Common mistakes

  • Using primitives. Collections store objects — use <Integer>, not <int>.
  • Expecting order from a HashSet/HashMap. They do not keep insertion order.
  • Forgetting the import. Import from java.util.
  • Choosing the wrong type. Match List/Set/Map to whether you need order, uniqueness or keys.

Summary

  • The Collections Framework provides ready data structures for objects.
  • List (ArrayList) keeps order and allows duplicates.
  • Set (HashSet) stores only unique items.
  • Map (HashMap) stores key-value pairs for fast lookup.
  • Collections grow automatically and offer many built-in methods.

Next, compare lists in ArrayList vs LinkedList, or maps in HashMap vs Hashtable.

Frequently Asked Questions

What is the Collections Framework in Java?
It is a set of ready-made classes and interfaces for storing and managing groups of objects — like lists, sets and maps. Instead of writing your own data structures, you use tested classes like ArrayList, HashSet and HashMap.
What is the difference between List, Set and Map?
A List keeps order and allows duplicates (ArrayList). A Set stores only unique items (HashSet). A Map stores key-value pairs (HashMap). You choose based on whether you need order, uniqueness, or key-based lookup.
Why use collections instead of arrays?
Arrays have a fixed size and few built-in operations. Collections grow and shrink automatically and offer methods to add, remove, search and sort easily. They are far more flexible for real programs.
What is the most commonly used collection?
ArrayList (a List) and HashMap (a Map) are the most used. ArrayList is a flexible, ordered list; HashMap gives fast lookup by key. Learning these two covers a large share of everyday needs.
Do collections store primitives?
No, collections store objects, so primitives are wrapped automatically — you use ArrayList<Integer>, not ArrayList<int>. Autoboxing handles the conversion for you.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicJava Interview Questions Your progress: 24 / 61 (39%) · Lesson 25 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free