0% found this document useful (0 votes)
4 views3 pages

Java_Collections_Full_Guide

The document provides a comprehensive guide to the Java Collections Framework, detailing various collection types such as List, Set, Map, Queue, and Deque, along with their specific implementations and use cases. It highlights key features like ordering, duplicates, and performance characteristics of collections like ArrayList, HashSet, and HashMap. Additionally, it covers common methods for manipulation and iteration, as well as thread safety considerations.

Uploaded by

Ritesh Bobhate
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)
4 views3 pages

Java_Collections_Full_Guide

The document provides a comprehensive guide to the Java Collections Framework, detailing various collection types such as List, Set, Map, Queue, and Deque, along with their specific implementations and use cases. It highlights key features like ordering, duplicates, and performance characteristics of collections like ArrayList, HashSet, and HashMap. Additionally, it covers common methods for manipulation and iteration, as well as thread safety considerations.

Uploaded by

Ritesh Bobhate
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
You are on page 1/ 3

Java Collections Framework - Full Guide with Examples

1. List - Ordered, Duplicates Allowed

- ArrayList: dynamic array (fast access, slow insert/remove)

Example:

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

list.add("A");

list.add("B");

list.add("A"); // Allowed

- LinkedList: faster insert/remove at head or tail

Example:

LinkedList<Integer> ll = new LinkedList<>();

ll.add(10); ll.addFirst(5);

- Vector: synchronized ArrayList (used less nowadays)

Vector<String> v = new Vector<>();

2. Set - No Duplicates Allowed

- HashSet: unordered, no duplicates

Set<String> set = new HashSet<>();

set.add("A"); set.add("A"); // Only one A stored

- LinkedHashSet: maintains insertion order

Set<String> lhs = new LinkedHashSet<>();

- TreeSet: stores in sorted (natural) order

Set<Integer> ts = new TreeSet<>();

ts.add(5); ts.add(1); // Sorted: 1, 5

3. Map - Key-Value Pairs

- HashMap: no order, fast, unique keys

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


map.put(1, "A");

map.put(2, "B");

- LinkedHashMap: insertion-order map

Map<Integer, String> lhm = new LinkedHashMap<>();

- TreeMap: keys sorted

Map<Integer, String> tm = new TreeMap<>();

4. Queue - FIFO Structure

- PriorityQueue: orders based on priority

Queue<Integer> pq = new PriorityQueue<>();

pq.add(3); pq.add(1); // poll() returns 1

5. Deque - Double-Ended Queue

Deque<Integer> dq = new ArrayDeque<>();

dq.addFirst(1); dq.addLast(2);

Common Methods:

- list.get(index), set.contains(val), map.get(key)

- map.entrySet(), map.keySet(), list.size()

Iteration:

- For-each loop: for (String s : list) { }

- Iterator: Iterator<String> it = list.iterator();

Thread Safe:

- Vector instead of ArrayList

- ConcurrentHashMap instead of HashMap

Use Case:

- ArrayList: Student roll numbers

- HashSet: Unique usernames


- HashMap: Roll no -> Student name

- TreeMap: Sorted phone directory

You might also like