Classes / Implementations
These are concrete classes that implement the interfaces.
List Implementations:
Class Characteristics ArrayList Resizable array, fast random access LinkedList Doubly linked list, better for insert/delete Vector Legacy, synchronized version of ArrayList Stack LIFO, extends Vector
Set Implementations:
Class Characteristics HashSet No duplicates, unordered LinkedHashSet Maintains insertion order TreeSet Sorted, no duplicates
Queue/Deque Implementations:
Class Characteristics PriorityQueue Elements sorted by natural order or comparator ArrayDeque Fast and efficient for both ends
Map Implementations:
Class Characteristics HashMap Unordered key-value pairs LinkedHashMap Maintains insertion order TreeMap Sorted by keys Hashtable Legacy, synchronized ConcurrentHashMap Thread-safe, high performance
Benefits of Using Java Collection Framework
- Reusability — Standardized data structures available out of the box.
- Interoperability — Interfaces allow switching implementations easily.
- Efficiency — Built-in optimized algorithms for sorting, searching, etc.
- Type Safety (via Generics) — Avoids
ClassCastException
. - Extensibility — Easy to extend or customize behavior.
- Thread-safety Options — Some implementations are thread-safe (e.g.,
ConcurrentHashMap
).
Common Qualities Among All Collection Classes
Generic support : All support Java Generics (e.g.,
List<String>
), ensuring type safety
Iterable : Most implement
Iterable
(except Map
), so can use enhanced for
loop
Fail-fast iterators : Many throw
ConcurrentModificationException
if modified during iteration
Null handling : Most allow nulls (with some exceptions like
TreeMap
or HashTable
)
Equality & hashing : Use
equals()
and hashCode()
to compare objects or store in hash-based collections
Comparison Summary Table

Example Code Snippets
Using an ArrayList
:
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list);
Using a HashSet
:
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Apple"); // Duplicate, won't be added
System.out.println(set);
Using a HashMap
:
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 2);
map.put("Banana", 3);
System.out.println(map);
1. ArrayList Example
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println(fruits.get(1)); // Banana
fruits.remove("Banana");
System.out.println(fruits.contains("Banana")); // false
System.out.println(fruits.size()); // 2
System.out.println(fruits); // [Apple, Orange]
}
}
2. LinkedList Example
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> animals = new LinkedList<>();
animals.add("Cat");
animals.add("Dog");
animals.addFirst("Horse");
System.out.println(animals); // [Horse, Cat, Dog]
animals.removeLast();
System.out.println(animals.contains("Cat")); // true
System.out.println(animals); // [Horse, Cat]
}
}
3. Vector Example
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<Integer> numbers = new Vector<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.removeElement(20);
System.out.println(numbers.contains(10)); // true
System.out.println(numbers.firstElement()); // 10
System.out.println(numbers); // [10, 30]
}
}
4. HashSet Example
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Red"); // Duplicate
System.out.println(colors.contains("Green")); // true
colors.remove("Green");
System.out.println(colors); // [Red]
}
}
5. LinkedHashSet Example
import java.util.LinkedHashSet;
public class LinkedHashSetExample {
public static void main(String[] args) {
LinkedHashSet<String> days = new LinkedHashSet<>();
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
System.out.println(days); // [Monday, Tuesday, Wednesday]
days.remove("Tuesday");
System.out.println(days.contains("Tuesday")); // false
}
}
6. TreeSet Example
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String[] args) {
TreeSet<Integer> scores = new TreeSet<>();
scores.add(90);
scores.add(70);
scores.add(80);
System.out.println(scores); // [70, 80, 90] (sorted)
System.out.println(scores.first()); // 70
scores.remove(80);
System.out.println(scores.contains(80)); // false
}
}
7. HashMap Example
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<String, Integer> marks = new HashMap<>();
marks.put("Math", 90);
marks.put("English", 85);
System.out.println(marks.get("Math")); // 90
marks.remove("English");
System.out.println(marks.containsKey("English")); // false
System.out.println(marks); // {Math=90}
}
}
8. LinkedHashMap Example
import java.util.LinkedHashMap;
public class LinkedHashMapExample {
public static void main(String[] args) {
LinkedHashMap<String, String> capitals = new LinkedHashMap<>();
capitals.put("USA", "Washington");
capitals.put("UK", "London");
capitals.put("India", "New Delhi");
System.out.println(capitals); // Keeps insertion order
capitals.remove("UK");
System.out.println(capitals.containsKey("UK")); // false
}
}
9. TreeMap Example
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
TreeMap<String, String> dictionary = new TreeMap<>();
dictionary.put("Apple", "A fruit");
dictionary.put("Book", "A source of knowledge");
dictionary.put("Car", "A vehicle");
System.out.println(dictionary); // Sorted by keys
System.out.println(dictionary.get("Book")); // A source of knowledge
dictionary.remove("Car");
System.out.println(dictionary.containsKey("Car")); // false
}
}
Here’s a detailed breakdown of the special qualities, when to use, and how to choose between:
LIST Implementations
Class Special Qualities When to Use ,
ArrayList — Backed by dynamic array- Fast random access (
get()
)- Slower insert/remove in the middle – When frequent access by index is needed- Best for read-heavy operations.
LinkedList – Doubly linked list- Fast insert/delete at start/end- Slower random access – When frequent insertions/deletions (especially at beginning/middle)- Use as a Queue or Deque
Vector – Synchronized (thread-safe)- Legacy class- Slower than
ArrayList
– When thread-safety is required in a legacy environment- Not recommended for new code
Choosing Between Them:
- Use ArrayList when you need fast access and minimal insert/delete.
- Use LinkedList when you modify the list frequently.
- Avoid Vector unless you require thread-safe legacy code.
SET Implementations
Class Special Qualities When to Use,
HashSet — No duplicates- Unordered- Backed by HashMap — When you need a set of unique items and don’t care about order
LinkedHashSet — Maintains insertion order- Slightly slower than HashSet — When you need unique items and care about order of insertion
TreeSet — Automatically sorted- No duplicates- Backed by TreeMap — When you need sorted unique elements
Choosing Between Them:
- Use HashSet for speed when order doesn’t matter.
- Use LinkedHashSet when you need predictable order.
- Use TreeSet when you need sorting automatically (e.g., names in alphabetical order).
MAP Implementations
Class Special Qualities When to Use ,
HashMap — Fast lookup with keys- No order maintained- Allows one null key — When fast key-value access is needed and order doesn’t matter
LinkedHashMap — Maintains insertion order- Slightly slower than HashMap — When you want fast access and predictable iteration order
TreeMap — Sorted by keys- Slower than HashMap- No null key — When keys must remain sorted (e.g., for range queries, alphabetical data)
Choosing Between Them:
- Use HashMap for general-purpose maps with fast access.
- Use LinkedHashMap for predictable iteration (e.g., LRU caching).
- Use TreeMap when sorted keys are necessary.
Decision Flow (When to Choose What)
If you need a list…
- Fast access by index →
ArrayList
- Frequent insert/delete at ends →
LinkedList
- Thread-safe (legacy) →
Vector
If you need unique elements (Set)…
- Don’t care about order →
HashSet
- Need insertion order →
LinkedHashSet
- Need sorted elements →
TreeSet
If you need key-value pairs (Map)…
- Fast access, no order needed →
HashMap
- Maintain insertion order →
LinkedHashMap
- Need sorted keys →
TreeMap
Thank you !