Last Updated: 28 May, 2022
The Java collection framework was introduced in the Java 1.2 version.
A Collection Framework provides an architecture to store and manipulate a group of objects as a single unit. The collection framework provides many interfaces and classes for implementing various data structures and algorithms.
In the Java collection framework, the java.util.Collection interface and the java.util.Map interface are the two main "root" interfaces of Java collection classes.
In the collections framework hierarchy, the Collection interface is the root interface, which was introduced in the Java 1.2 version and placed in the java.util package.
All the collection framework classes and interfaces directly or indirectly implement the Collection interface.
A Collection Interface provides many methods to perform the basic operations such as add, remove, clear, etc., which are used by all the child classes of the collection interface.
public interface Collection<E> extends Iterable<E>
Methods & Descriptions |
---|
boolean add(E e) |
boolean addAll(Collection<? extends E> c) |
void clear() |
boolean contains(Object o) |
boolean containsAll(Collection<?> c) |
boolean equals(Object o) |
int hashCode() |
boolean isEmpty() |
Iterator<E> iterator() |
default Stream<E> parallelStream() |
boolean remove(Object o) |
boolean removeAll(Collection<?> c) |
default boolean removeIf(Predicate<? super E> filter) |
boolean retainAll(Collection<?> c) |
int size() |
default Spliterator<E> spliterator() |
default Stream |
Object[] toArray() |
<T> T[] toArray(T[] a) |
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What are the advantages of the Java Collection Framework?
Ans. The Java collection framework provides the following benefits:
What is the difference between Iterator and Enumeration in Java?
Ans. Though both Iterator and Enumeration allow us to traverse over elements of Collections in Java, there are some significant differences between them:
Enumeration | Iterator |
---|---|
Enumeration is a legacy interface that was introduced in JDK 1.0. | Iterator is not a legacy interface; it was introduced in JDK 1.2. |
Enumeration does not allow us to remove elements from a collection during traversal. | Iterator also allow us to remove elements from collection during traversal. It has a remove() method. |
Enumeration supports only legacy classes like Vector, Stack, Hashtable, Properties, Dictionary. | Iterator supports all the Collection framework classes. |
public interface Enumeration<E> | public interface Iterator<E> |
hasMoreElement() nextElement() | hasNext() next() remove() |
In summary, both Enumeration and Iterator will give successive elements, but Iterator is a new and improved version where method names are shorter and it has a new method called remove.
What is the difference between an Iterator and a ListIterator in Java?
Ans. Iterator and ListIterator are both used for traversing purposes in Java, but there are many differences between them that we will see in the given below table.
Iterator | ListIterator |
---|---|
It can traverse only in the forward direction using an iterator. | We can use ListIterator to traverse List only. |
We can traverse in only forward direction using Iterator. | Using ListIterator, we can traverse a List in both directions (forward and backward). |
We cannot obtain indexes while using an Iterator. | We can obtain indexes at any point of time while traversing a list using ListIterator. The methods nextIndex() and previousIndex() are used for this purpose. |
We cannot add elements to a collection while traversing it using an iterator. It throws a ConcurrentModificationException when we try to do it. | We can add an element at any point of time while traversing a list using ListIterator. |
We cannot replace the existing element value when using an Iterator. | By using the set(E e) method of ListIterator, we can replace the last element returned by the next() or previous() methods. |
public interface Iterator<E> | public interface ListIterator<E> extends Iterator<E> |
hasNext(), next(), remove() | add(E e), hasNext(), hasPrevious(), next(), nextIndex(), previous(), previousIndex(), remove(), set(E e) |
What are the differences between Collection and Collections in Java?.
Ans. Collection and Collections are both available in the java.util package, but there are many differences between them.
Collection | Collections |
---|---|
Collection is an interface. | Collections is a class. |
It represents a group of objects as a single entity. | It is a utility class that provides various utility methods for collection objects. |
It is the root interface of the Collection framework. | It is just a utility class. |
It is used to derive the data structures of the Collection framework. | It contains various static methods which help in data structure manipulation. |