0% found this document useful (0 votes)
2 views5 pages

Java Collection Framework

The Java Collection Framework provides a dynamic way to store and manipulate groups of objects, overcoming the limitations of fixed-size arrays. It includes a hierarchy of interfaces such as List, Set, and Map, each serving different purposes, like maintaining order or ensuring uniqueness. Key components include ArrayList for dynamic arrays, HashSet for unique elements, and HashMap for key-value pairs, along with sorting mechanisms using Comparable and Comparator.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Java Collection Framework

The Java Collection Framework provides a dynamic way to store and manipulate groups of objects, overcoming the limitations of fixed-size arrays. It includes a hierarchy of interfaces such as List, Set, and Map, each serving different purposes, like maintaining order or ensuring uniqueness. Key components include ArrayList for dynamic arrays, HashSet for unique elements, and HashMap for key-value pairs, along with sorting mechanisms using Comparable and Comparator.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Collection Framework

1. Introduction to Collection Framework

In the early days of Java, we used Arrays to store multiple objects. But arrays have a "Fixed
Size" problem—if you create an array of 10, you can't easily add an 11th item.

The Collection Framework is a unified architecture that provides ready-made interfaces and
classes to store and manipulate a group of objects dynamically.

Real-Life Example: Think of a Wardrobe. An Array is like a fixed wooden shelf. A


Collection is like a modern expandable rack where you can add or remove sections as your
clothes increase.

2. The Hierarchy (The Family Tree)

The framework is organized into a hierarchy of interfaces.

 Iterable (Root): Allows us to use for-each loops.


 Collection Interface: The mother interface for List, Set, and Queue.
 Map Interface: A separate hierarchy for Key-Value pairs.

3. The List Interface (Ordered & Duplicates Allowed)

A List is like a notebook where you write entries line by line. You can write the same thing
twice, and the order matters.

A. ArrayList (Dynamic Array)

It uses an array internally that grows automatically.

 Best for: Random access/searching data.


 Real-Life Example: A Shopping List. You add items as you remember them, and you
can have "Milk" listed twice.

Programming Code:

Java
import [Link].*;

public class ArrayListDemo {


public static void main(String[] args) {
ArrayList<String> shoppingList = new ArrayList<>();
[Link]("Milk");
[Link]("Eggs");
[Link]("Milk"); // Duplicates allowed
[Link]("Shopping items: " + shoppingList);
[Link]("Item at index 1: " + [Link](1));
}
}

Output:

Plaintext
Shopping items: [Milk, Eggs, Milk]
Item at index 1: Eggs

B. LinkedList

It uses a doubly linked list where each element (Node) points to the next and previous one.

 Best for: Frequent insertion and deletion.


 Real-Life Example: A Train. Adding or removing a compartment in the middle is
easy; you just change the hooks (links).

4. Stack & Queue

Stack (LIFO - Last In First Out)

 Real-Life Example: A Stack of Plates. The last plate you put on top is the first one
you pick up.
 Method: push() to add, pop() to remove.

Queue (FIFO - First In First Out)

 Real-Life Example: A Cinema Ticket Line. The first person in the line is the first one
to get a ticket.

5. The Set Interface (Unique Elements Only)

A Set is like a club membership. You are either in or out; you can't be a member twice.

A. HashSet

 Feature: Unordered, no duplicates. Uses "Hashing" for speed.


 Real-Life Example: Student IDs. You cannot have two students with the same ID in
a university.
B. TreeSet

 Feature: Stores elements in Natural Sorted Order (A-Z or 1-10).


 Real-Life Example: A Dictionary. Words are always sorted alphabetically.

Programming Code:

Java
import [Link].*;

public class SetDemo {


public static void main(String[] args) {
TreeSet<Integer> rollNumbers = new TreeSet<>();
[Link](50);
[Link](10);
[Link](30);
[Link](10); // Ignored (Duplicate)

[Link]("Sorted Unique Roll Nos: " + rollNumbers);


}
}

Output:

Plaintext
Sorted Unique Roll Nos: [10, 30, 50]

6. Map Interface (Key-Value Pairs)

A Map stores data as Key = Value. Keys must be unique.

A. HashMap

 Real-Life Example: A Library Index.


o Key: Book ISBN (Unique)
o Value: Book Name

Programming Code:

Java
import [Link].*;

public class MapDemo {


public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
[Link](101, "Java");
[Link](102, "Python");

[Link]("Course for ID 101: " + [Link](101));


}
}

Output:

Plaintext
Course for ID 101: Java

7. Sorting: Comparable vs Comparator

When sorting custom objects (like a Student class), Java needs to know how to compare them.

 Comparable: "Natural" sorting. The class sorts itself (e.g., sorting Students by Roll
No).
 Comparator: "External" sorting. You create a separate class to sort differently (e.g.,
sorting Students by Name or Marks).

8. Properties Class

The Properties class is a subclass of Hashtable. It is used to store configuration data in a


.properties file (like database credentials).

 Format: username=admin, password=123.


Key words for learning purpose.

Concept Best Used For Duplicates? Sorted?

ArrayList Fetching data by index Yes No

LinkedList Adding/Removing data Yes No

HashSet Searching unique data No No

TreeSet Maintaining unique sorted data No Yes

HashMap Key-based lookup No (Keys) No

TreeMap Key-based lookup (sorted keys) No (Keys) Yes

You might also like