Interfaces and Classes in the Java Collections Framework
- Interfaces:
Collection
,List
,Set
,Queue
,Map
,SortedSet
, andSortedMap
. - Common Classes:
ArrayList
,LinkedList
,HashSet
,TreeSet
,HashMap
,LinkedHashMap
,PriorityQueue
,Stack
.
Key Interfaces
- Collection Interface:
- Purpose: The root of the collection hierarchy. It provides general methods for basic collection operations like adding, removing, and checking elements.
-
Common Subinterfaces:
List
Set
Queue
- List Interface:
- Purpose: Represents an ordered collection (sequence) that allows duplicate elements. Lists allow positional access and insertion of elements.
-
Implementing Classes:
-
ArrayList
LinkedList
Vector
(includingStack
)- Set Interface:
- Purpose: Represents a collection that does not allow duplicate elements. Sets are typically used for fast access to unique elements.
-
Implementing Classes:
-
HashSet
LinkedHashSet
TreeSet
- Queue Interface:
- Purpose: Represents a collection designed for holding elements prior to processing. Typically follows First-In-First-Out (FIFO) ordering.
-
Implementing Classes:
-
LinkedList
(also implementsList
) PriorityQueue
ArrayDeque
- Map Interface:
- Purpose: Represents a collection of key-value pairs, where each key is unique.
Map
is not a true collection but part of the Collections Framework. -
Implementing Classes:
-
HashMap
LinkedHashMap
TreeMap
Hashtable
ConcurrentHashMap
- SortedSet Interface:
- Purpose: Extends
Set
to handle sorted sets of elements. It maintains elements in a specific order, typically their natural order. -
Implementing Classes:
-
TreeSet
- SortedMap Interface:
- Purpose: Extends
Map
to handle sorted key-value pairs. The keys are maintained in ascending order. - Implementing Classes:
TreeMap
Commonly Used Classes
- ArrayList:
- Purpose: Resizable array implementation of the
List
interface. Provides fast random access to elements. - Use Case: Best for read-heavy operations where quick access by index is required.
- LinkedList:
- Purpose: Doubly-linked list implementation of the
List
andQueue
interfaces. Provides efficient insertions and deletions. - Use Case: Best for scenarios where frequent insertions and deletions are needed, especially at the beginning or end of the list.
- HashSet:
- Purpose: Implements the
Set
interface using a hash table. It offers constant-time performance for basic operations. - Use Case: Best for storing unique elements with no specific ordering.
- TreeSet:
- Purpose: Implements the
Set
interface using a tree structure. It maintains elements in a sorted order. - Use Case: Best for storing unique elements in a sorted order.
- HashMap:
- Purpose: Implements the
Map
interface using a hash table. It offers constant-time performance for storing and retrieving key-value pairs. - Use Case: Best for fast access to key-value pairs without concern for order.
- LinkedHashMap:
- Purpose: Extends
HashMap
and maintains a linked list of the entries in the map, preserving the order of insertion. - Use Case: Best when you need a map that maintains insertion order.
- PriorityQueue:
- Purpose: Implements the
Queue
interface with elements ordered by their natural ordering or by a comparator. - Use Case: Best for scenarios where elements need to be processed in a specific order, such as in scheduling tasks.
- Stack:
- Purpose: Extends
Vector
and represents a last-in-first-out (LIFO) stack of objects. - Use Case: Best for implementing algorithms that require a LIFO stack, such as depth-first search.
Follow-up Question
- When would you prefer using
LinkedList
overArrayList
? - Answer: When you need to perform frequent insertions and deletions, particularly at the beginning or end of the list.