0% found this document useful (0 votes)
12 views38 pages

Java Collections Expanded

The document provides a detailed guide on Java Collections, covering various types such as Lists, Stacks, Queues, Sets, and Maps. It explains the purpose of collections, their interfaces, and when to use each type, along with examples for clarity. Additionally, it includes performance notes, best practices, and real-world applications of these data structures.

Uploaded by

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

Java Collections Expanded

The document provides a detailed guide on Java Collections, covering various types such as Lists, Stacks, Queues, Sets, and Maps. It explains the purpose of collections, their interfaces, and when to use each type, along with examples for clarity. Additionally, it includes performance notes, best practices, and real-world applications of these data structures.

Uploaded by

eidabdirahman34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Java Collections

• Detailed Guide: Lists, Stacks, Queues,


PriorityQueues, Sets and Maps
Agenda
• What is Collection, Lists (ArrayList, LinkedList),
Stacks, Queues, PriorityQueues, Sets (HashSet,
LinkedHashSet, TreeSet), Maps (HashMap,
LinkedHashMap, TreeMap)
What is a Collection?
• Collections group multiple objects together.
They simplify working with dynamic data and
avoid manual array resizing.
Why Use Collections?
• Collections provide flexible, reusable, efficient
data storage and manipulation.
Collection Interface
• Defines basic operations like add, remove,
size, iterator. Root of List, Set, Queue.
List
• Ordered collection allowing duplicates.
Examples: ArrayList for quick reads, LinkedList
for fast inserts.
ArrayList Explained
• Stores elements in resizable array. Fast
random access, slow insert/delete in middle.
ArrayList Example
• ArrayList<String> list = new ArrayList<>();\
[Link]("A");\[Link]("B");\nOutput: [A, B]
LinkedList Explained
• Nodes linked together. Fast insert/remove at
start/middle. Slower random access.
LinkedList Example
• LinkedList<Integer> list = new LinkedList<>();\
[Link](1);\[Link](2);\nOutput: [1,
2]
Iterator & forEach
• Iterator lets you safely loop and remove
elements. forEach is modern Java style.
Iterator Example
• Iterator<String> it = [Link]();\
nwhile([Link]())
{[Link]([Link]());}
When to Use List
• Choose ArrayList for indexed access. LinkedList
for frequent insertion/removal.
Stack
• LIFO structure. Last added = first removed.
Example: Undo in editors.
Stack Example
• Stack<Integer> s = new Stack<>();\[Link](1);
[Link](2); [Link]();\nOutput: 2 removed, 1
remains.
Stack Real Life
• Undo operations, expression evaluation,
function call stack.
Queue
• FIFO structure. First in = first out. Example:
print queue, customer service line.
Queue Example
• Queue<String> q = new LinkedList<>();\
[Link]("A"); [Link]();\nOutput: A removed.
PriorityQueue
• Elements ordered by natural order or
Comparator. Example: ER patients by severity.
PriorityQueue Example
• PriorityQueue<Integer> pq = new
PriorityQueue<>();\[Link](5); [Link](1);
[Link]();\nOutput: 1 removed first.
Set
• No duplicates allowed. HashSet: fast,
unordered. LinkedHashSet: keeps insertion
order. TreeSet: sorted.
HashSet Example
• Set<String> s = new HashSet<>();\[Link]("A");
[Link]("A");\nOutput: [A]
LinkedHashSet Example
• LinkedHashSet<String> lhs = new
LinkedHashSet<>();\[Link]("B");
[Link]("A");\nOutput: [B, A]
TreeSet Example
• TreeSet<Integer> ts = new TreeSet<>();\
[Link](2); [Link](1);\nOutput: [1, 2]
Map
• Key-value pairs. HashMap: fast, unordered.
LinkedHashMap: keeps insertion order.
TreeMap: keys sorted.
HashMap Example
• Map<String, String> map = new
HashMap<>();\[Link]("US", "United
States");\nOutput: {US=United States}
LinkedHashMap Example
• LinkedHashMap keeps insertion order of keys.
TreeMap Example
• TreeMap sorts keys naturally. Example:
country codes sorted.
When to Use Maps
• HashMap: quick lookup. TreeMap: sorted
keys. LinkedHashMap: ordered output.
Tips for Collections
• Always choose based on operation cost:
access, insert, remove, sort.
Performance Notes
• ArrayList: O(1) access. LinkedList: O(n) access.
HashSet: O(1) lookup. TreeSet: O(log n) sort.
Pitfalls
• Do not modify collection while iterating
without Iterator's remove().
Good Practices
• Use interfaces like List, Set, Map for flexibility.
Prefer immutables when possible.
Real World Uses
• Lists: to-do list. Stack: browser back. Queue:
printer jobs. Map: phonebook.
Quiz
• What is the difference between HashSet and
TreeSet? When use LinkedList over ArrayList?
More Practice
• Write a program to count word occurrences
using Map.
Summary
• Java Collections simplify data storage and
manipulation with flexibility and power.
Thank You
• Questions?

You might also like