Collections in JAVA.
Collections in Java are part of the Java Collections Framework, which provides a set of classes and interfaces for storing and manipulating groups of objects. Collections simplify the handling of data structures like lists, sets, queues, and maps by offering ready-to-use methods for common operations such as searching, sorting, inserting, and deleting elements.
Key Interfaces in the Java Collections Framework
Collection: The root interface for all collection classes.
- Subinterfaces:
List,Set,Queue, etc.
- Subinterfaces:
List:
- Ordered collection of elements.
- Allows duplicate elements.
- Common implementations:
ArrayList(dynamic array-based implementation).LinkedList(doubly linked list implementation).Vector(synchronized array-based implementation).
Set:
- Collection that does not allow duplicate elements.
- Common implementations:
HashSet(unordered, backed by a hash table).LinkedHashSet(maintains insertion order).TreeSet(sorted, backed by a red-black tree).
Queue:
- Used to hold elements prior to processing.
- Follows FIFO (First In, First Out) in most cases.
- Common implementations:
PriorityQueue(elements sorted by natural ordering or a comparator).LinkedList(also implements theQueueinterface).
Deque:
- Double-ended queue that allows insertion and removal from both ends.
- Common implementations:
ArrayDeque.LinkedList.
Map (Not a part of
Collectioninterface but part of the framework):- Maps keys to values.
- Does not allow duplicate keys.
- Common implementations:
HashMap(unordered, backed by a hash table).LinkedHashMap(maintains insertion order).TreeMap(sorted, backed by a red-black tree).Hashtable(synchronized implementation).
Important Classes in Java Collections Framework
Collections: A utility class with static methods for operating on collections, like
sort,reverse,shuffle,binarySearch, etc.Arrays: Utility class for operations on arrays, such as
sort,binarySearch, and converting arrays to lists.
Example Code
Here’s an example of using some common collection classes:

Comments
Post a Comment