1. Overview
In this tutorial, we'll learn how to use Iterator forEachRemaining() method in java 8 efficiently when compared to the older java versions.
Iterator is an interface from collections api and is used to iterate over the collections objects.
Iterator methods:
The following methods are present in the Iterator interface till java 7.
boolean hasNext() E next() default void remove()
2. How to use Iterator for each before JDK 8
If you are not using Java 8 then it is little not convenient to use the hasNext(), next() method every time.
While loop is needed to iterate the collection over it to get the values.
Look at the below code before java 8.
Example 1:
package com.javaprogramto.java8.iterate; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class IterateForEachExample { public static void main(String[] args) { List<String> strings = Arrays.asList("A", "B", "C", "D"); Iterator<String> it = strings.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
Output:
A B C D
3. Java 8 Iterator forEachRemaining() Example
In java 8, forEachRemaining() method is introduced instead of using two methods over while loop with older java versions.
forEachRemaining() method takes the Consumer as argument which gets the values from the iterator.
This way code is simplified.
Example 2:
In the below example, forEachRemaining() method is used to print the list of strings onto the console.
package com.javaprogramto.java8.iterate; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.function.Consumer; public class IterateForEachRemainingExample { public static void main(String[] args) { // inline consumer System.out.println("Inline consumer - "); List<String> strings = Arrays.asList("A", "B", "C", "D"); Iterator<String> it = strings.iterator(); it.forEachRemaining(value -> System.out.println(value)); // with separate consumer Set<Integer> values = new HashSet<>(); values.add(100); values.add(200); values.add(300); values.add(400); System.out.println("With custom consumer - "); Consumer<Integer> consumer = (number) -> System.out.println(number - 1); Iterator<Integer> iterator = values.iterator(); iterator.forEachRemaining(consumer); } }
Output:
Inline consumer - A B C D With custom consumer - 399 99 199 299
4. Java forEachRemaining() With SplitIterator
Example 3:
package com.javaprogramto.java8.iterate; import java.util.HashSet; import java.util.Set; import java.util.Spliterator; import java.util.function.Consumer; public class SplitIteratorForEachRemainingExample { public static void main(String[] args) { Set<Integer> values = new HashSet<>(); values.add(100); values.add(200); values.add(300); values.add(400); Spliterator<Integer> splitIterator = values.stream().spliterator(); Consumer<Integer> consumer = (number) -> System.out.println(number - 1); splitIterator.forEachRemaining(consumer); } }
Output:
399 99 199 299
5. Conclusion
In this article, we've seen what is forEachRemaining() in java 8 with Iterator and SplitIterator.
No comments:
Post a Comment
Please do not add any spam links in the comments section.