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

Java Collections

Uploaded by

titisulshi
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)
3 views5 pages

Java Collections

Uploaded by

titisulshi
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 Collections

The Java Collections Framework (JCF) is a set of classes and interfaces designed to handle
and manipulate groups of objects in a standardized way. Collections simplify the task of
managing data by providing ready-made implementations of common data structures like lists,
sets, queues, and maps.

Key Features of the Collections Framework


1. Unified Architecture:
Provides a consistent interface for different types of collections (e.g., List , Set , Map ).
All collections extend from the Collection interface (except Map ).
2. Generics:
Introduced in Java 5 to provide type safety at compile-time.
Example: List<String> ensures that only String objects are added.
3. Thread Safety:
The framework includes both thread-safe and non-thread-safe classes.
Thread-safe classes include Vector , Hashtable , and
[Link]() .
4. Extensibility:
You can create custom collections by extending framework classes.

Core Interfaces of the Collections Framework


1. Collection Interface:
The root interface for most collections.
Subinterfaces: List , Set , Queue , Deque .
2. List Interface:
An ordered collection that allows duplicate elements.
Common implementations: ArrayList , LinkedList , Vector .
3. Set Interface:
A collection that does not allow duplicate elements.
Common implementations: HashSet , LinkedHashSet , TreeSet .
4. Queue Interface:
A collection for holding elements prior to processing (FIFO by default).
Common implementations: PriorityQueue , LinkedList (also implements Deque ).
5. Deque Interface:
A double-ended queue that allows adding/removing elements from both ends.
Common implementations: ArrayDeque , LinkedList .
6. Map Interface:
Represents a collection of key-value pairs.
Does not extend the Collection interface.
Common implementations: HashMap , LinkedHashMap , TreeMap , Hashtable .

Common Classes and Their Uses


1. ArrayList:
Implements List .
Resizable array, fast for random access, slower for insertion/deletion in the middle.
Example:

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


[Link]("Apple");
[Link]("Banana");
[Link]("Apple"); // Duplicates allowed

2. LinkedList:
Implements both List and Deque .
Doubly linked list, better for frequent insertions and deletions.
Example:

LinkedList<String> list = new LinkedList<>();


[Link]("Orange");
[Link]("Mango");
[Link]("Grapes");

3. HashSet:
Implements Set .
No duplicate elements, no guaranteed order.
Example:
Set<Integer> set = new HashSet<>();
[Link](1);
[Link](2);
[Link](1); // Duplicate ignored

4. TreeSet:
Implements Set .
Elements are sorted in natural order or using a comparator.
Example:

TreeSet<String> treeSet = new TreeSet<>(); [Link]("Zebra");


[Link]("Apple"); [Link]("Monkey");

5. HashMap:
Implements Map .
Stores key-value pairs; no duplicate keys; values can be duplicate.
Does not guarantee order.
Example:

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


[Link]("John", 25);
[Link]("Alice", 30);

6. TreeMap:

Implements Map .
Keys are sorted in natural order or using a comparator.
Example:

TreeMap<String, Integer> treeMap = new TreeMap<>();


[Link]("John", 25);
[Link]("Alice", 30);

Things to Consider When Using Collections


1. Choosing the Right Collection:
List for ordered and indexed data.
Set for unique elements.
Map for key-value pairs.
Queue for elements to be processed in a specific order.
2. Performance:
Use ArrayList for faster random access.
Use LinkedList for frequent insertions/removals.
Use HashMap and HashSet for fast lookups (O(1)).
3. Thread Safety:
Use [Link]() or ConcurrentHashMap for thread-safe
operations.
4. Generics:
Use generics to avoid runtime errors and improve type safety.
5. Immutability:
Use [Link]() or [Link]() (Java 9+) to create immutable
collections.
6. Comparator and Comparable:
Use Comparator for custom sorting and Comparable for natural sorting.
7. Memory Usage:
Be cautious of the memory overhead of HashMap and HashSet due to hashing.

Practice Questions
Theory Questions

1. What are the core interfaces in the Java Collections Framework?


2. Explain the difference between ArrayList and LinkedList .
3. How does HashMap handle collisions?
4. What is the difference between HashSet and TreeSet ?
5. How does ArrayDeque differ from LinkedList when used as a queue?

Coding Exercises

1. Write a program to find duplicate elements in a List using a HashSet .


2. Create a TreeSet of custom objects (e.g., Employee ) and sort them by a property like age
using a Comparator .
3. Write a program to demonstrate the use of ConcurrentHashMap for a thread-safe key-value
store.
4. Implement a PriorityQueue to manage tasks with different priorities.
5. Create a HashMap and iterate through its keys and values using different approaches (e.g.,
for-each , Iterator ).

You might also like