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

Java Collection Framework Cheat Sheet

The document provides an overview of the Java Collection Framework, detailing the hierarchy of collections, including List, Set, Queue, and Map interfaces. It describes the characteristics and use cases of various implementations like ArrayList, HashSet, and HashMap, along with common methods associated with each collection type. Additionally, it offers guidance on when to use specific collections based on performance needs and data characteristics.
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)
6 views3 pages

Java Collection Framework Cheat Sheet

The document provides an overview of the Java Collection Framework, detailing the hierarchy of collections, including List, Set, Queue, and Map interfaces. It describes the characteristics and use cases of various implementations like ArrayList, HashSet, and HashMap, along with common methods associated with each collection type. Additionally, it offers guidance on when to use specific collections based on performance needs and data characteristics.
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 Collection Framework Cheat Sheet

1. Collection Hierarchy

Collection (interface)
/ | \
List Set Queue
| | |
ArrayList HashSet LinkedList (also List)
LinkedList LinkedHashSet
Vector TreeSet (sorted)
Stack

Map (interface)
/ \
HashMap TreeMap
LinkedHashMap
Hashtable

2. List

List is ordered and allows duplicates.

- ArrayList: Resizable array, fast random access


- LinkedList: Doubly linked list, fast insertion/deletion
- Vector: Thread-safe, legacy
- Stack: LIFO stack (extends Vector)

Sample:
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list.get(1)); // Output: Banana

3. Set

Set is unordered and does not allow duplicates.

- HashSet: Fast, no order


- LinkedHashSet: Maintains insertion order
- TreeSet: Sorted set

Sample:
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple");
System.out.println(set); // Output: [Apple, Banana]
Java Collection Framework Cheat Sheet

4. Queue / Deque

Queue is FIFO. Deque can be used as stack or queue.

- LinkedList: Implements Queue and Deque


- PriorityQueue: Sorted queue
- ArrayDeque: Fast, resizable

Sample:
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
System.out.println(queue.poll()); // Output: 1

5. Map

Map stores key-value pairs.

- HashMap: Unordered, allows null key


- LinkedHashMap: Maintains insertion order
- TreeMap: Sorted keys
- Hashtable: Synchronized, legacy

Sample:
Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
System.out.println(map.get(1)); // Output: One

6. Common Methods

Collection:
add(e), remove(e), contains(e), size(), isEmpty(), clear(), iterator()

List:
get(index), set(index, e), add(index, e), indexOf(e)

Queue:
offer(e), poll(), peek()

Map:
put(key, value), get(key), remove(key), containsKey(key), keySet(), values(), entrySet()

7. When to Use What


Java Collection Framework Cheat Sheet

- Fast random access: ArrayList


- Frequent insertion/removal: LinkedList
- No duplicates: HashSet
- Sorted data: TreeSet / TreeMap
- Key-value storage: HashMap
- Ordered keys: LinkedHashMap
- Thread-safe: ConcurrentHashMap / Vector
- Stack: Stack or ArrayDeque
- Queue: LinkedList / PriorityQueue

You might also like