[post-views]
java.util
package.
The Java Collection Framework consists of three major components:
Let us see the Java collections framework hierarchy.
Iterator
provides a way to access elements of a collection sequentially without exposing the underlying structure.
Iterator has the following three methods
boolean hasNext()
: Returns true
if the iteration has more elements.E next()
: Returns the next element in the iterationvoid remove()
: Removes the last element returned by the iterator (optional operation).
Collection
interface is implemented by various sub-interfaces like List
, Set
, and Queue
Below is a summary table listing the common methods provided by the Collection
interface in Java:
Method Signature | Description |
---|---|
boolean add(E e) |
Adds the specified element to the collection (optional operation). |
void clear() |
Removes all of the elements from this collection (optional operation). |
boolean contains(Object o) |
Returns true if this collection contains the specified element. |
boolean equals(Object o) |
Compares the specified object with this collection for equality. |
boolean isEmpty() |
Returns true if this collection contains no elements. |
Iterator<E> iterator() |
Returns an iterator over the elements in this collection. |
boolean remove(Object o) |
Removes a single instance of the specified element from this collection, if it is present (optional operation). |
int size() |
Returns the number of elements in this collection. |
Object[] toArray() |
Returns an array containing all of the elements in this collection. |
List
can contain duplicate elements.
List
interface includes the following classes
ArrayList and LinkedList are widely used in Java programming. The Vector class has been deprecated since Java 5.
It extends the Collection
interface and represents a collection that cannot contain duplicate elements.
Set
does not allow duplicate elements. Each element in a Set
must be unique.Set
are not stored in any particular order. However, some implementations, like LinkedHashSet
, maintain insertion order.Set
allow one null element (e.g., HashSet
), but some do not (e.g., TreeSet
).
There are three main implementations of the Set interface:
The Queue interface has the following classes
Interface | Description | Key Methods |
---|---|---|
Iterator | An object for iterating over a collection. | hasNext() , next() , remove() |
Collection | The root interface in the collection hierarchy. | add(E e) , remove(Object o) , size() , clear() , contains(Object o) , isEmpty() , iterator() |
List | An ordered collection (sequence) that allows duplicate elements. | get(int index) , set(int index, E element) , add(int index, E element) , remove(int index) , indexOf(Object o) , lastIndexOf(Object o) , listIterator() |
Set | A collection that does not allow duplicate elements. | Inherited methods from Collection: add(E e) , remove(Object o) , size() , clear() , contains(Object o) , isEmpty() , iterator() |
SortedSet | A Set that maintains its elements in ascending order. | first() , last() , comparator() , subSet(E fromElement, E toElement) , headSet(E toElement) , tailSet(E fromElement) |
NavigableSet | A SortedSet with navigation methods reporting closest matches for given search targets. | lower(E e) , floor(E e) , ceiling(E e) , higher(E e) , pollFirst() , pollLast() , descendingSet() |
Queue | A collection is used to hold multiple elements before processing, typically in a FIFO (First-In-First-Out) manner. | offer(E e) , poll() , peek() , remove() , element() |
Deque | A double-ended queue that allows element insertion and removal at both ends. | addFirst(E e) , addLast(E e) , removeFirst() , removeLast() , getFirst() , getLast() , offerFirst(E e) , offerLast(E e) , pollFirst() , pollLast() , peekFirst() , peekLast() |
Map | An object that maps keys to values, with no duplicate keys allowed. | put(K key, V value) , get(Object key) , remove(Object key) , containsKey(Object key) , containsValue(Object value) , keySet() , values() , entrySet() |
SortedMap | A Map that maintains its mappings in ascending key order. | comparator() , subMap(K fromKey, K toKey) , headMap(K toKey) , tailMap(K fromKey) , firstKey() , lastKey() |
NavigableMap | A SortedMap with navigation methods returning the closest matches for given search targets. | lowerEntry(K key) , floorEntry(K key) , ceilingEntry(K key) , higherEntry(K key) , firstEntry() , lastEntry() , pollFirstEntry() , pollLastEntry() , descendingMap() |
ListIterator | An Iterator for lists that allows bidirectional traversal and modification of elements. | hasPrevious() , previous() , add(E e) , set(E e) , previousIndex() , nextIndex() |
Comparable | Provides a natural ordering for objects of a class. | compareTo(T o) |
Comparator | Defines a custom ordering for objects. | compare(T o1, T o2) , reversed() |
You must be logged in to submit a review.