Java Iterator Vs. Listiterator Vs. SpliteratorIterator in JavaIt allows us to iterate through a collection's elements one after the other in a sequential fashion. It is a key component of the Java Collections Framework and works with lists, sets, queues, and maps, among other collection types. Because the iterator is unidirectional, it can only move in one direction. Features of Iterator
IteratorExample.java Output: Ram Ravi Raghu Explanation A List<String> is used to represent the list of names in the Iterator example. We use the list's iterator() method to generate an Iterator. The Iterator provides a straightforward method for iteratively going through the list's elements. As long as there are more elements (iterator.hasNext()), the while loop will keep running. We use an iterator.next() to obtain and output each element within the loop. This example shows how to use an iterator to get elements in a collection sequentially. ListIterator in JavaIn Java, a sub-interface of the Iterator that is particularly made for lists is called ListIterator. By offering bidirectional traversal and the capacity to change the list while iterating, it expands the possibilities of the Iterator. Features of ListIterator
ListIteratorExample.java Output: Red Green Blue Blue Green Red Explanation The ListIterator example focuses on traversing and modifying lists in both directions. A List<String> is what we use to represent our list of colors. Using the listIterator() function of the list, we are able to obtain a ListIterator. The code's first section uses listIterator.hasNext() and listIterator.next() to illustrate forward traversal. Using listIterator.hasPrevious() and listIterator.previous(), backward traversal is demonstrated in the second section. ListIterator is more flexible than a basic Iterator since it has extra methods (add(), remove(), and set()) for altering the list as it is being traversed. Spliterator in JavaAs part of the Stream API, Java 8 brought the Spliterator interface. Large datasets can be processed more effectively in parallel thanks to its capabilities for parallel traversal and element splitting. Features of Spliterator
SpliteratorExample.java Output: 1 2 3 4 5 Explanation We operate with a list of integers in the Spliterator example, which is represented by a List<Integer>. To get a Spliterator, utilize the spliterator() function. Spliterator is meant to be processed in parallel; sequential and parallel traversal are both demonstrated in the code. For sequential traversal, use the forEachRemaining() method; for parallel processing, break the Spliterator into two pieces using trySplit(). This example shows how to use Spliterator in the context of a list in a basic way. Spliterator is very helpful when working with large datasets and concurrent streams. Differnece between Iterator, Listiterator, and Spliterator
Next TopicPure Functions in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India