0% found this document useful (0 votes)
5 views8 pages

Java Collection Framework

The document provides a comprehensive overview of the Java Collection Framework, detailing its interfaces, concrete classes, and legacy classes. It categorizes collections into Lists, Sets, and Maps, explaining their characteristics and implementations. Additionally, it includes examples of common operations and methods available for manipulating collections.

Uploaded by

s54479391
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)
5 views8 pages

Java Collection Framework

The document provides a comprehensive overview of the Java Collection Framework, detailing its interfaces, concrete classes, and legacy classes. It categorizes collections into Lists, Sets, and Maps, explaining their characteristics and implementations. Additionally, it includes examples of common operations and methods available for manipulating collections.

Uploaded by

s54479391
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/ 8

Here is a complete list of types in the Java Collection Framework, grouped by category for clarity:

1. Interfaces
Interface Description
Core Interfaces
Collection Root interface for most collections
List Ordered collection with duplicates
Set Unordered collection with no duplicates
SortedSet A Set with elements in sorted order
NavigableSet A SortedSet with navigation methods
Queue Collection for holding elements prior to processing
Deque Double-ended queue
Map Interfaces
Map Key-value pairs
SortedMap Map with keys in sorted order
NavigableMap SortedMap with navigation methods

2. Concrete Classes
Class Description
List Implementations
ArrayList Resizable array
LinkedList Doubly linked list
Vector Synchronized, legacy
Stack LIFO stack (extends Vector)
Set Implementations
HashSet Unordered set
LinkedHashSet Maintains insertion order
TreeSet Sorted set (Red-Black tree)
EnumSet For enum types only
CopyOnWriteArraySet Thread-safe version of Set
Queue / Deque Implementations
PriorityQueue Elements ordered by priority
ArrayDeque Resizable array double-ended queue
LinkedList Also implements Deque
ConcurrentLinkedQueue Thread-safe, non-blocking queue
LinkedBlockingQueue Blocking queue
ArrayBlockingQueue Bounded blocking queue
PriorityBlockingQueue Thread-safe priority queue
DelayQueue Elements with delayed availability
SynchronousQueue Transfer handoff between threads
Map Implementations
HashMap Unordered key-value pairs
LinkedHashMap Maintains insertion order
TreeMap Sorted map
Hashtable Legacy synchronized map
Properties Special Hashtable for config
ConcurrentHashMap Thread-safe hash map
WeakHashMap Entries garbage-collected if key is weak
IdentityHashMap Uses == instead of equals()
EnumMap Key type is an enum

3. Legacy Classes

Class Description
Vector Synchronized dynamic array
Stack LIFO stack (extends Vector)
Hashtable Synchronized map
Dictionary Abstract class for key-value pairs
Enumeration Legacy iterator

1. List
 Ordered, allows duplicates
 Maintains insertion order
 Access elements via index (get(index))
 Example implementations: ArrayList, LinkedList, Vector
2. Set
 Unordered, no duplicates
 Does not allow duplicate elements
 Implementations differ in ordering
 Example implementations: HashSet, LinkedHashSet, TreeSet
3. Map
 Stores key-value pairs
 Keys must be unique, but values can duplicate
 Not a subtype of Collection
 Example implementations: HashMap, TreeMap, Hashtable
4. ArrayList
 Implements: List
 Dynamic array, fast random access
 Slower insertions/deletions in the middle
 Not synchronized
5. HashSet
 Implements: Set
 No duplicates, no order
 Internally backed by a HashMap
 Fast lookup, insert, delete (O(1) avg.)
6. TreeSet
 Implements: NavigableSet, SortedSet
 Sorted order (ascending) by default
 No duplicates
 Backed by a Red-Black Tree
 Slower than HashSet (O(log n))
8. HashMap
 Implements: Map
 Unordered key-value pairs
 Allows one null key and multiple null values
 Fast (O(1) avg. time for get/put)
9. TreeMap
 Implements: NavigableMap, SortedMap
 Sorted by keys
 Does not allow null keys
 Backed by Red-Black Tree (O(log n))
10. HashTable
 Legacy class, implements Map
 Thread-safe, synchronized
 Does not allow null keys or values
 Slower than HashMap (due to synchronization)
11. LinkedList
 Implements: List, Deque, Queue
 Doubly-linked list
 Good for frequent insert/delete operations
 Slower for random access than ArrayList
12. LinkedHashSet
 Implements: Set
 Maintains insertion order
 Like HashSet but with predictable iteration order

Feature List Set Map

Stores Elements Unique elements Key-value pairs

Allows Duplicates Yes No Duplicate keys, values


1.

ArrayList, in LinkedHashSet, in in LinkedHashMap, in


Order Maintained
LinkedList) HashSet HashMap

Access by Index Yes No by Key

ArrayList,
Examples HashSet, TreeSet HashMap, TreeMap
LinkedList
Feature ArrayList LinkedList

Structure Dynamic array Doubly-linked list

Access Time Fast (O(1)) Slow (O(n))

Insert/Delete (middle) Slow (O(n)) Fast (O(1))

Memory Usage Less (contiguous) More (extra pointers)

Best Use Frequent access Frequent insert/remove

Feature HashSet TreeSet LinkedHashSet

Sorted (natural
Ordering Unordered Insertion order
order)

Slower (O(log
Performance Fast (O(1)) Fast (O(1))
n))

Allows null Yes (one) No Yes (one)

Use case Fast + no order Need sorting Fast + order preserved


Feature HashMap TreeMap LinkedHashMap Hashtable

Ordering Unordered Sorted by key Insertion order Unordered

1 null key, many No null


Null Keys/Values No null key Same as HashMap
null values key/value

Thread-Safe No No No Yes

Performance O(1) O(log n) O(1) Slow (sync)

Sorted map Legacy thread-


Use case General-purpose Access-order or LRU
needed safe

Feature HashMap ConcurrentHashMap HashSet


Type Map (key-value pair) Thread-safe Map (key-value pair) Set (unique values only)
Yes (High performance, segment
Thread-Safe? No No
locking)
Null Keys
Yes (one null key) No Yes (one null)
Allowed?
Null Values
Yes No Yes
Allowed?
Ordering No order No order No order
Duplicates Keys unique, values can Keys unique, values can
No duplicates
Allowed? duplicate duplicate
Underlying Bucket array + Same as HashMap + Segment Backed by a HashMap
Structure LinkedList/Tree Locking internally
General-purpose key- Fast set operations with
Use Case Multi-threaded key-value access
value storage no duplicates
1. sort() – Sort a List

List<String> names = new ArrayList<>(List.of("Zara", "Amit", "Raj"));

Collections.sort(names); // Sorts alphabetically

System.out.println(names); // [Amit, Raj, Zara]

2. sort() with Comparator

List<Integer> nums = new ArrayList<>(List.of(5, 1, 10));

Collections.sort(nums, Comparator.reverseOrder());

System.out.println(nums); // [10, 5, 1]

3. binarySearch() – Search in a Sorted List

List<String> list = new ArrayList<>(List.of("Amit", "Raj", "Zara"));

Collections.sort(list); // Binary search requires sorted list

int index = Collections.binarySearch(list, "Raj");

System.out.println(index); // 1

4. reverse() – Reverse a List

List<Integer> list = new ArrayList<>(List.of(1, 2, 3));

Collections.reverse(list);

System.out.println(list); // [3, 2, 1]

5. shuffle() – Randomize Elements

List<String> list = new ArrayList<>(List.of("A", "B", "C"));

Collections.shuffle(list);

System.out.println(list); // e.g., [C, A, B]

6. rotate() – Rotate List Elements

List<Integer> list = new ArrayList<>(List.of(1, 2, 3, 4, 5));

Collections.rotate(list, 2); // Moves last 2 elements to front

System.out.println(list); // [4, 5, 1, 2, 3]
7. swap() – Swap Elements in a List

List<String> list = new ArrayList<>(List.of("One", "Two", "Three"));

Collections.swap(list, 0, 2);

System.out.println(list); // [Three, Two, One]

8. replaceAll() – Replace All Occurrences

List<String> list = new ArrayList<>(List.of("Apple", "Banana", "Apple"));

Collections.replaceAll(list, "Apple", "Orange");

System.out.println(list); // [Orange, Banana, Orange]

9. fill() – Replace All with Same Value

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3));

Collections.fill(list, 0);

System.out.println(list); // [0, 0, 0]

10. frequency() – Count Occurrences

List<String> list = new ArrayList<>(List.of("A", "B", "A", "C"));

int count = Collections.frequency(list, "A");

System.out.println(count); // 2

11. min() and max() – Find Extremes

List<Integer> list = new ArrayList<>(List.of(10, 5, 20));

System.out.println(Collections.min(list)); // 5

System.out.println(Collections.max(list)); // 20

12. synchronizedList() – Thread-safe List

List<String> list = Collections.synchronizedList(new ArrayList<>());

list.add("Hello");
13. unmodifiableList() – Read-only List

List<String> list = new ArrayList<>(List.of("A", "B"));

List<String> readOnly = Collections.unmodifiableList(list);

// readOnly.add("C"); // Throws UnsupportedOperationException

14. emptyList() – Immutable Empty List

List<String> empty = Collections.emptyList();

System.out.println(empty); // []

15. singleton() – One-Element Set

Set<String> single = Collections.singleton("OnlyOne");

System.out.println(single); // [OnlyOne]

16. singletonList() – One-Element List

List<String> singleList = Collections.singletonList("Hello");

System.out.println(singleList); // [Hello]

17. singletonMap() – One Key-Value Pair

Map<String, String> map = Collections.singletonMap("key", "value");

System.out.println(map); // {key=value}

You might also like