Iterator vs Collection in Java Last Updated : 01 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Iterator and Collection, both has helped and comforted the programmers at many a times. But their usage and application has a very wide difference. 1. Iterator Declaration public interface Iterator Type Parameters: E - the type of elements returned by this iteratorIterators are used in Collection framework in Java to retrieve elements one by one. Method Summary Modifier and TypeMethodDescriptiondefault voidforEachRemaining(Consumer<? super E> action)Performs the given action for each remaining element until all elements have been processed or the action throws an exception.booleanhasNext()Returns true if the iteration has more elements.Enext()Returns the next element in the iteration.default voidremove()Removes from the underlying collection the last element returned by this iterator (optional operation). 2. Collection Declaration: public interface Collection<E> extends Iterable<E> Type Parameters: E - the type of elements returned by this iteratorA Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit. Method Summary Modifier and TypeMethodDescriptionbooleanadd(E e)Ensures that this collection contains the specified element (optional operation).booleanaddAll(Collection<? extends E> c)Adds all of the elements in the specified collection to this collection (optional operation).voidclear()Removes all of the elements from this collection (optional operation).booleancontains(Object o)Returns true if this collection contains the specified element.booleancontainsAll(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.booleanequals(Object o)Compares the specified object with this collection for equality.inthashCode()Returns the hash code value for this collection.booleanisEmpty()Returns true if this collection contains no elements.Iterator<E>iterator()Returns an iterator over the elements in this collection.default Stream<E>parallelStream()Returns a possibly parallel Stream with this collection as its source.booleanremove(Object o)Removes a single instance of the specified element from this collection, if it is present (optional operation).booleanremoveAll(Collection<?> c)Removes all of this collection's elements that are also contained in the specified collection (optional operation).default booleanremoveIf(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.booleanretainAll(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).intsize()Returns the number of elements in this collection.default Spliterator<E>spliterator()Creates a Spliterator over the elements in this collection.default Stream<E>stream()Returns a sequential Stream with this collection as its source.Object[]toArray()Returns an array containing all of the elements in this collection.T[]toArray(T[] a)Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. Iterator Vs. Collection Iterator can only move to next() element or remove() an element. However Collection can add(), iterate, remove() or clear() the elements of the collection. Iterator provides a better speed than Collections, as the Iterator interface has limited number of operations. java.sql.SQLException extends Iterable. Hence it allows the caller to safely iterate over causes of SQLException. Using a collection, in this case, would be expensive because, in a chain of n exceptions, use of a collection in the SQLException interface would potentially require the construction of O(n^2) elements. However, use of Iterable provides O(n) access to the exception chain. Comment More infoAdvertise with us Next Article Iterator vs Collection in Java R RishabhPrabhu Follow Improve Article Tags : Java Java-Collections Java - util package Practice Tags : JavaJava-Collections Similar Reads Collection Interface in Java The Collection interface in Java is a core member of the Java Collections Framework located in the java.util package. It is one of the root interfaces of the Java Collection Hierarchy. The Collection interface is not directly implemented by any class. Instead, it is implemented indirectly through it 6 min read Iterator vs Foreach In Java Background : Iterator is an interface provided by collection framework to traverse a collection and for a sequential access of items in the collection. // Iterating over collection 'c' using iterator for (Iterator i = c.iterator(); i.hasNext(); ) System.out.println(i.next()); For eachloop is meant f 4 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read C++ STL vs Java Collections Framework Java and C++ provide their different functionalities and their use cases. When it comes to Data structures and Algorithms Java has a special framework named Java collection framework and C++ provides a library called STL.So let's have a look at what are the different functions, complexities, and wha 3 min read Convert an Iterable to Collection in Java Iterable and Collection have served to be of great use in Java. Iterators are used in Collection framework in Java to retrieve elements one by one and a Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and inte 4 min read How to use Iterator in Java? 'Iterator' is an interface which belongs to collection framework. It allows us to traverse the collection, access the data element and remove the data elements of the collection. java.util package has public interface Iterator and contains three methods: boolean hasNext(): It returns true if Iterato 3 min read Convert Iterator to Iterable in Java Given an Iterator, the task is to convert it into Iterables in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: By overriding the abstract method Iterable.itera 3 min read Convert an Iterator to a List in Java Given an Iterator, the task is to convert it into List in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: Naive Approach: Get the Iterator. Create an empty lis 2 min read Iterator Interface In Java Java Iterator Interface of java collections allows us to access elements of the collection and is used to iterate over the elements in the collection(Map, List or Set). It helps to easily retrieve the elements of a collection and perform operations on each element. Iterator is a universal iterator a 6 min read AbsractCollection iterator() Method in Java with Examples The iterator() method of Java AbstractCollection is used to return an iterator of the same elements as that of the Collection. The elements are returned in random order from what was present in the Collection. Syntax: Iterator iterate_value = AbstractCollection.iterator(); Parameters: The function d 2 min read Like