Java Collections
Overview
Java Collections provide a framework for storing and manipulating groups
of objects. They offer a range of data structures for various purposes,
including lists, sets, maps, queues, and deques.
by Amin Bhuiyan Ratul
List Interface
The List interface provides a way to store an ordered sequence of elements.
Elements can be accessed by index. This allows for duplicate elements.
1 ArrayList 2 LinkedList
A resizable array A linked list implementation.
implementation. It's efficient It's efficient for insertions and
for random access. deletions at the beginning or
end of the list.
3 Vector 4 Stack
A synchronized version of A Last-In, First-Out (LIFO)
ArrayList. It's thread-safe. data structure. It's
implemented as a subclass of
Vector.
Set Interface
The Set interface represents a collection of unique elements. It doesn't allow
duplicate elements and maintains no specific order.
HashSet LinkedHashSet
Uses a hash table for efficient Maintains the insertion order of
lookups. It's unordered and elements. It's implemented as a
doesn't guarantee element hash table with a linked list.
order.
TreeSet
Uses a binary search tree to store elements in a sorted order. It
provides efficient search and retrieval.
Map Interface
The Map interface stores data in key-value pairs. Each key must be unique, and it allows for efficient retrieval of values based on
keys.
HashMap LinkedHashMap TreeMap
Uses a hash table for fast lookups. It's Maintains insertion order of key-value Uses a binary search tree to store key-
unordered and doesn't guarantee key- pairs. It's implemented as a hash table value pairs in a sorted order based on
value order. with a linked list. keys. It's efficient for sorted lookups.
Queue Interface
The Queue interface represents a First-In, First-Out (FIFO) data structure.
Elements are added to the rear and removed from the front.
Offer
1 Adds an element to the queue.
Peek
2 Retrieves the element at the front of the queue without
removing it.
Poll
3 Retrieves and removes the element at the front of the queue.
Deque Interface
The Deque interface extends Queue and allows adding and removing
elements from both ends. It's a double-ended queue.
Methods Description
addFirst(e) Inserts an element at the
beginning of the deque.
addLast(e) Inserts an element at the end of
the deque.
removeFirst() Removes and returns the first
element.
removeLast() Removes and returns the last
element.
Sorting Collections
Collections can be sorted using the [Link]() method, which employs the merge sort algorithm by default.
Comparable Interface 1
Defines a natural ordering of elements using the
compareTo() method.
2 Comparator Interface
Allows for custom sorting based on specific criteria. It
provides the compare() method for comparisons.
SortedSet and SortedMap 3
These interfaces guarantee that elements are
maintained in sorted order. They use the Comparable
or Comparator interface for sorting.
Iterating Collections
The Iterator interface provides a way to traverse through the elements of a
collection. It offers methods for iterating over elements and checking if there
are more elements available.
for-each loop
An enhanced for loop that simplifies iteration through collections.
Iterator
Provides methods like hasNext(), next(), and remove() for iteration.
ListIterator
Offers additional methods for bidirectional iteration and element
modifications.
Modifying Collections
Collections can be modified by adding, removing, or replacing elements. Different methods are available for specific operations.
add(e) remove(e) set(index, e)
Adds an element to the collection. Removes an element from the collection. Replaces an element at a specified index.
Common Collection
Operations
Java Collections provide various utility methods for common operations like
searching, copying, and comparing collections.
1 contains(e) 2 isEmpty()
Checks if a collection Determines if the collection is
contains a specific element. empty.
3 size() 4 toArray()
Returns the number of Converts a collection into an
elements in the collection. array.