Collections Framework Overview

Site Admin · 11 Sep 2026 · 11 views

Data Structures, Ready-Made

The Collections Framework in java.util provides ready-made implementations of the most useful data structures, so you do not have to write linked lists or hash maps yourself.

The Three Main Interfaces

InterfaceWhat it isPopular examples
ListOrdered sequence, allows duplicatesArrayList, LinkedList
SetUnique elements, no duplicatesHashSet, TreeSet
MapKey-value pairsHashMap, TreeMap

List in Practice

import java.util.*;

List<String> cities = new ArrayList<>();
cities.add("Delhi");
cities.add("Pune");
cities.add("Delhi");   // duplicates allowed
System.out.println(cities.get(1)); // Pune
System.out.println(cities.size()); // 3

Set in Practice

Set<String> unique = new HashSet<>();
unique.add("apple");
unique.add("banana");
unique.add("apple");   // ignored - no duplicates
System.out.println(unique); // [apple, banana]

Map in Practice

Map<String, Integer> scores = new HashMap<>();
scores.put("Asha", 92);
scores.put("Bimal", 85);
System.out.println(scores.get("Asha"));   // 92
System.out.println(scores.containsKey("Bimal")); // true

Choosing the Right Collection

  • ArrayList: fast random access, fast append.
  • LinkedList: fast insert/delete at ends for big lists.
  • HashSet: fastest uniqueness checks; no order.
  • TreeSet: unique elements kept sorted.
  • HashMap: fastest key lookup; unordered.
  • TreeMap: keys kept in sorted order.

Iterating

for (String c : cities) {
    System.out.println(c);
}
for (Map.Entry<String, Integer> e : scores.entrySet()) {
    System.out.println(e.getKey() + " = " + e.getValue());
}

Java collection framework

Key Points

  • Lists allow duplicates, Sets do not, Maps pair keys with values.
  • Choose the simplest structure that meets your needs.
  • Use generic types List<String> to keep collections type safe.
Share this post:

Comments (0)

Please login or register to comment.