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:
ListSetQueue- List Interface:
- Purpose: Represents an ordered collection (sequence) that allows duplicate elements. Lists allow positional access and insertion of elements.
-
Implementing Classes:
-
ArrayList LinkedListVector(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 LinkedHashSetTreeSet- 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) PriorityQueueArrayDeque- Map Interface:
- Purpose: Represents a collection of key-value pairs, where each key is unique.
Mapis not a true collection but part of the Collections Framework. -
Implementing Classes:
-
HashMap LinkedHashMapTreeMapHashtableConcurrentHashMap- SortedSet Interface:
- Purpose: Extends
Setto handle sorted sets of elements. It maintains elements in a specific order, typically their natural order. -
Implementing Classes:
-
TreeSet - SortedMap Interface:
- Purpose: Extends
Mapto 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
Listinterface. 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
ListandQueueinterfaces. 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
Setinterface 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
Setinterface 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
Mapinterface 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
HashMapand 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
Queueinterface 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
Vectorand 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
LinkedListoverArrayList? - Answer: When you need to perform frequent insertions and deletions, particularly at the beginning or end of the list.