Bytes
rocket

Your Success, Our Mission!

6000+ Careers Transformed.

Introduction to Collections

Last Updated: 22nd March, 2026

6.1 Introduction to Collections

6.1.1 Explanation

The Java Collections Framework (JCF) provides a unified architecture for storing, manipulating, and retrieving groups of objects efficiently.
Introduced in Java 2 (JDK 1.2), the framework standardizes data structure handling by offering ready-to-use classes and interfaces such as ListSetMap, and Queue.

Collections replace primitive arrays with dynamic, resizable, and type-safe data containers, improving performance and maintainability.
They handle everything from ordered sequences to unique element sets and key-value mappings, enabling developers to implement complex algorithms without manually managing data storage.

The framework resides under java.util and supports both generic types (for compile-time type safety) and iterators (for sequential traversal).
Common collection operations include insertion, deletion, searching, sorting, and iteration.
Understanding when to use each collection type — based on order, uniqueness, and access pattern — is critical for writing efficient code.

6.1.2 Code

import java.util.*;

public class CollectionIntro {

public static void main(String[] args) {

List<String> list = new ArrayList<>();

list.add("Java");

list.add("Python");

list.add("C++");

System.out.println("List: " + list);

Set<String> set = new HashSet<>(list);

System.out.println("Set (unique elements): " + set);

Map<Integer, String> map = new HashMap<>();

map.put(1, "Apple");

map.put(2, "Banana");

map.put(3, "Cherry");

System.out.println("Map: " + map);

}

}

6.1.3 Example

Output:

List: [Java, Python, C++]

Set (unique elements): [C++, Java, Python]

Map: {1=Apple, 2=Banana, 3=Cherry}

Explanation:
A List maintains order and allows duplicates, while a Set automatically removes duplicates and doesn’t maintain insertion order.
A Map stores key-value pairs, providing fast lookup by key. These three are the cornerstones of the Java Collections Framework.

6.1.4 Use Cases

  • Managing large data collections dynamically (e.g., user lists, transaction records).

  • Implementing caches, dictionaries, and lookup tables.

  • Removing duplicates from datasets.

  • Structuring hierarchical or relational data in applications.

     

6.1.5 Relevant Table

Interface

Implementation

Ordered

Duplicates

Thread-Safe

Example Use

List

ArrayList, LinkedList

Yes

Yes

No

Ordered collections

Set

HashSet, TreeSet

No

No

No

Unique elements

Map

HashMap, TreeMap

No

Keys unique

No

Key-value storage

Queue

LinkedList, PriorityQueue

Yes

Yes

No

Task scheduling

6.2 Lists and Sets

6.2.1 Explanation

Lists in Java are ordered collections that allow duplicate elements. Each element has an index, enabling random access and modifications.
The most common implementations are:

  • ArrayList: Backed by a dynamic array; fast for access, slower for insertions.
  • LinkedList: Backed by a doubly linked list; efficient for frequent insertions/deletions.

Sets, on the other hand, are unordered collections that ensure all elements are unique.
The major implementations include:

  • HashSet: Fastest implementation, based on hashing.
  • LinkedHashSet: Maintains insertion order.
  • TreeSet: Sorts elements in natural or custom order using a comparator.

Choosing between these depends on performance requirementsordering needs, and data uniqueness constraints.

6.2.2 Code

import java.util.*;

public class ListSetExample {

public static void main(String[] args) {

List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 3, 8, 3, 1));

System.out.println("ArrayList: " + numbers);

Set<Integer> uniqueNumbers = new HashSet<>(numbers);

System.out.println("HashSet (unique values): " + uniqueNumbers);

Set<Integer> sortedNumbers = new TreeSet<>(numbers);

System.out.println("TreeSet (sorted unique): " + sortedNumbers);

}

}

6.2.3 Example

Output:

ArrayList: [5, 3, 8, 3, 1]

HashSet (unique values): [1, 3, 5, 8]

TreeSet (sorted unique): [1, 3, 5, 8]

Explanation:
The ArrayList keeps duplicates, HashSet removes duplicates, and TreeSet automatically sorts unique elements.
This shows how different collection types serve distinct purposes with minimal code changes.

6.2.4 Use Cases

  • ArrayList: Storing ordered data like student lists or logs.

  • LinkedList: Implementing queues and stacks.

  • HashSet: Removing duplicates in search results.

  • TreeSet: Maintaining sorted leaderboards or rankings.

     

6.2.5 Relevant Table

Collection

Data Structure

Ordered

Duplicates

Time Complexity (Add/Search)

ArrayList

Dynamic Array

Yes

Yes

O(1)/O(n)

LinkedList

Doubly Linked List

Yes

Yes

O(1)/O(n)

HashSet

Hash Table

No

No

O(1)/O(1)

TreeSet

Red-Black Tree

Yes (Sorted)

No

O(log n)/O(log n)

6.3 Maps and Iteration

6.3.1 Explanation

Maps store data in key-value pairs, allowing fast retrieval of values using keys.
Unlike other collections, Map does not extend Collection but stands as a separate interface under java.util.

Major implementations include:

  • HashMap: Unordered, fast, non-synchronized.
  • LinkedHashMap: Maintains insertion order.
  • TreeMap: Stores entries in sorted order by keys.

Iteration through a Map can be done using entry setskey sets, or lambda expressions. Java 8’s forEach() method and enhanced Iterator API allow elegant and efficient traversal.
Map is heavily used in databases, caching, and any situation requiring key-based lookups.

6.3.2 Code

import java.util.*;

public class MapIteration {

public static void main(String[] args) {

Map<String, Integer> scores = new HashMap<>();

scores.put("Alice", 90);

scores.put("Bob", 85);

scores.put("Charlie", 95);

System.out.println("Scores Map: " + scores);

// Iterating using entrySet

for (Map.Entry<String, Integer> entry : scores.entrySet()) {

System.out.println(entry.getKey() + " → " + entry.getValue());

}

// Using forEach (Java 8+)

scores.forEach((key, value) -> System.out.println("Lambda: " + key + " scored " + value));

}

}

6.3.3 Example

Output:

Scores Map: {Alice=90, Bob=85, Charlie=95}

Alice → 90

Bob → 85

Charlie → 95

Lambda: Alice scored 90

Lambda: Bob scored 85

Lambda: Charlie scored 95

Explanation:
This example demonstrates iterating over a Map using both traditional and functional-style loops.
Maps enable efficient lookup and manipulation of related key-value pairs.

6.3.4 Use Cases

  • Building dictionaries or configuration maps.
  • Managing relational mappings (e.g., Employee ID → Name).
  • Implementing caches and lookup tables.
  • Counting frequencies (word counts, product usage).

6.3.5 Relevant Table

Map Type

Ordering

Null Keys

Performance

Example

HashMap

Unordered

One allowed

O(1)

General purpose

LinkedHashMap

Insertion order

One allowed

O(1)

LRU cache

TreeMap

Sorted by keys

Not allowed

O(log n)

Sorted mapping

Module 6: Collections FrameworkIntroduction to Collections

Top Tutorials

Logo
Computer Science

CNN in Deep Learning 2026

A beginner-friendly guide to CNNs: understand deep learning essentials, create Python-based models, and explore advanced applications.

4 Modules12 Lessons151 Learners
Start Learning
Logo
Computer Science

Breaking The Limits: Scaling Databases with MySQL Partitioning

Learn MySQL partitioning with examples. Improve query performance, scalability, and data management using RANGE, LIST, HASH, KEY, and composite techniques.

7 Modules11 Lessons71 Learners
Start Learning
Logo

ML in Action: Hands-On Guide to Deploying and Serving Models

Learn model deployment and serving—from concepts to real-world architectures, tools, APIs, containers, and cloud workflows for production-ready ML.

4 Modules10 Lessons78 Learners
Start Learning
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2026 AlmaBetter