0% found this document useful (0 votes)
2 views

Collections in Java

The Java Collections Framework provides a set of classes and interfaces for implementing reusable data structures like lists, sets, queues, and maps, facilitating efficient data handling. Key interfaces include List, Set, Map, and Queue, each with specific characteristics regarding order and duplication. The framework also offers utility methods for operations such as sorting and supports thread-safe collections.

Uploaded by

SachinJadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Collections in Java

The Java Collections Framework provides a set of classes and interfaces for implementing reusable data structures like lists, sets, queues, and maps, facilitating efficient data handling. Key interfaces include List, Set, Map, and Queue, each with specific characteristics regarding order and duplication. The framework also offers utility methods for operations such as sorting and supports thread-safe collections.

Uploaded by

SachinJadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

📚 Java Collections Framework (JCF)

The Java Collections Framework is a set of classes and interfaces that


implement commonly reusable data structures such as lists, sets,
queues, and maps. It is part of the java.util package and is essential
for handling and storing groups of objects efficiently.

✅ Why Use Collections?


• Store, retrieve, and manipulate data easily

• Replace arrays with more flexible data structures

• Built-in sorting, searching, and iteration

• Reduce boilerplate code

🧩 Key Interfaces in Java Collections


Common
Interface Description
Implementations
Ordered collection ArrayList,
List
(duplicates allowed) LinkedList
Unordered collection HashSet, TreeSet,
Set
(no duplicates) LinkedHashSet
LinkedList,
Queue FIFO structure
PriorityQueue
HashMap, TreeMap,
Map Key-value pairs
LinkedHashMap

🔹 Common Collection Classes


1. List

Maintains insertion order and allows duplicates.

java
CopyEdit
import java.util.*;

public class ListExample {


public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // Duplicates allowed
System.out.println(fruits);
}
}

2. Set

No duplicates, can be unordered or sorted.

java
CopyEdit
import java.util.*;

public class SetExample {


public static void main(String[] args) {
Set<String> names = new HashSet<>();
names.add("Alice");
names.add("Bob");
names.add("Alice"); // Duplicate ignored
System.out.println(names);
}
}

3. Map

Stores key-value pairs. Keys must be unique.

java
CopyEdit
import java.util.*;

public class MapExample {


public static void main(String[] args) {
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);
ages.put("Alice", 28); // Replaces previous
value
System.out.println(ages);
}
}

4. Queue

Follows FIFO (First In, First Out).

java
CopyEdit
import java.util.*;

public class QueueExample {


public static void main(String[] args) {
Queue<String> tasks = new LinkedList<>();
tasks.add("Task1");
tasks.add("Task2");
System.out.println(tasks.poll()); // Removes and
returns head
System.out.println(tasks);
}
}

🧠 Utility Class: Collections


Java provides a utility class java.util.Collections for operations like
sorting, reversing, shuffling, etc.

java
CopyEdit
import java.util.*;

public class SortExample {


public static void main(String[] args) {
List<Integer> nums = Arrays.asList(3, 1, 2);
Collections.sort(nums);
System.out.println(nums); // [1, 2, 3]
}
}

🔐 Synchronized Collections
To make collections thread-safe:

• Use Collections.synchronizedList(...)

• Or use concurrent collections like ConcurrentHashMap

Summary Table
Allows Maintains
Type Thread-Safe Sorted
Duplicates Order
ArrayList Yes Yes No No
LinkedList Yes Yes No No
HashSet No No No No
TreeSet No Yes (sorted) No Yes
HashMap Keys: No No No No
TreeMap Keys: No Yes (sorted) No Yes

You might also like