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

Collection Framework in Java

The Collection Framework in Java provides a unified architecture for storing and manipulating groups of objects, featuring interfaces, implementations, and algorithms for efficient data management. It includes various data structures such as List, Set, Queue, and Map, each with specific characteristics and performance metrics. Understanding this framework enhances programming efficiency and problem-solving capabilities in Java.

Uploaded by

kiran dhainje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Collection Framework in Java

The Collection Framework in Java provides a unified architecture for storing and manipulating groups of objects, featuring interfaces, implementations, and algorithms for efficient data management. It includes various data structures such as List, Set, Queue, and Map, each with specific characteristics and performance metrics. Understanding this framework enhances programming efficiency and problem-solving capabilities in Java.

Uploaded by

kiran dhainje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Collection Framework in Java

1. Introduction

The Collection Framework in Java is a unified architecture for storing and manipulating
groups of objects. It includes interfaces, implementations (classes), and algorithms that help
manage data efficiently.

Key Features:

• Provides ready-to-use data structures.

• Supports generics, iteration, and sorting.

• Offers thread-safe implementations.

• Improves code reusability and performance.

2. Hierarchy of Collection Framework

1. Interfaces (Root Interface: Collection<E>)

• List (Ordered, allows duplicates) → ArrayList, LinkedList, Vector, Stack

• Set (Unordered, unique elements) → HashSet, LinkedHashSet, TreeSet

• Queue (FIFO structure) → PriorityQueue, Deque, ArrayDeque

• Map (Key-Value pairs) → HashMap, LinkedHashMap, TreeMap, Hashtable

3. Collection Interfaces and Classes

1. List Interface (Ordered Collection with Duplicates Allowed)

• ArrayList

• Implements a dynamic array.

• Faster for searching (O(1) for index-based access).

• Slower for insertion/deletion in the middle (O(n)).

• LinkedList

• Implements a doubly linked list.

• Faster for insertions/deletions (O(1) at head/tail).

• Slower for searching (O(n)).

• Vector

• Similar to ArrayList, but thread-safe (synchronized).

• Stack (LIFO)
• Inherits Vector.

• Methods: push(), pop(), peek(), empty().

2. Set Interface (Unique Elements, No Duplicates)

• HashSet

• Uses hashing, unordered, allows null.

• Fast operations (O(1)).

• LinkedHashSet

• Maintains insertion order.

• TreeSet

• Uses Red-Black Tree (Sorted order, O(log n)).

• Does not allow null values.

3. Queue Interface (FIFO, Exception Handling Methods)

• PriorityQueue

• Elements processed based on priority.

• Uses Min-Heap (natural order sorting).

• ArrayDeque (Double-ended queue)

• Faster than Stack and LinkedList.

• Methods: offer(), poll(), peek(), push(), pop().

4. Map Interface (Key-Value Pair Collection)

• HashMap

• Uses hashing (O(1) for put/get operations).

• Allows one null key, multiple null values.

• LinkedHashMap

• Maintains insertion order.

• TreeMap

• Sorted by keys (Red-Black Tree, O(log n)).

• Does not allow null keys.

• Hashtable
• Thread-safe, no null keys/values.

4. Iterators in Collection

• Iterator Interface: hasNext(), next(), remove().

• ListIterator (Bidirectional for List).

• forEach() loop and Streams API.

5. Comparators and Sorting

• Comparable<T>: Implements compareTo(T obj) (Natural sorting).

• Comparator<T>: Implements compare(T o1, T o2) (Custom sorting).

• Collections.sort() for sorting lists.

6. Important Utility Classes (java.util.Collections)

• Collections.sort(list): Sorts a list.

• Collections.reverse(list): Reverses the list.

• Collections.shuffle(list): Shuffles elements.

• Collections.min(list), Collections.max(list): Finds min/max.

• Collections.synchronizedList(list): Thread-safe list.

7. Thread-Safety in Collections

• synchronizedList(), synchronizedSet(), synchronizedMap().

• CopyOnWriteArrayList, ConcurrentHashMap for better performance.

8. Differences Between Collection Classes

Feature ArrayList LinkedList HashSet TreeSet HashMap TreeMap

Yes
Order Yes Yes No (sorted) No Yes (sorted)

Keys - No, Keys - No,


Duplicates Yes Yes No No Values - Yes Values - Yes

Performance
(Search) O(1) O(n) O(1) O(log n) O(1) O(log n)

Performance
(Insert/Delete) O(n) O(1) O(1) O(log n) O(1) O(log n)
9. Conclusion

• Use List when order matters and duplicates are allowed.

• Use Set when unique elements are needed.

• Use Queue for FIFO or priority-based processing.

• Use Map for key-value pairs.

Understanding the Collection Framework helps in efficient programming and problem-


solving in Java.

You might also like