35

and both of them get a Consumer as parameter. so if Java 8, is meant to avoid confusions, like it has done in Time API, why has it added a new confusion? or am I missing some point?

mostafa.S
  • 1,452
  • 4
  • 16
  • 27
  • 3
    are you trying to compare `Iterator` and `Iterable`? http://stackoverflow.com/questions/6863182/what-is-the-difference-between-iterator-and-iterable-and-how-to-use-them – Naman Feb 26 '17 at 07:35
  • 1
    @JBNizet all right guys, don't throw tomatoes on me! I got it :D – mostafa.S Feb 26 '17 at 08:31

4 Answers4

105

To understand why the two methods both exist, you need to first understand what are Iterator and Iterable.

An Iterator basically is something that has a "next element" and usually, an end.

An Iterable is something that contains elements in a finite or infinite sequence and hence, can be iterated over by keep getting the next element. In other words, Iterables can be iterated over by Iterators.

Now that you understand this, I can talk about what's the difference between the two methods in question.

Let's use an array list as an example. This is what is inside the array list:

[1, 3, 6, 8, 0]

Now if I call Iterable.forEach() and pass in System.out::print(), 13680 will be printed. This is because Iterable.forEach iterates through the whole sequence of elements.

On the other hand, if I get the Iterator of the array list and called next twice, before calling forEachRemaining with System.out::print(), 680 will be printed. The Iterator has already iterated through the first two elements, so the "remaining" ones are 6, 8 and 0.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    all right. thanks for your clear and complete answer. – mostafa.S Feb 26 '17 at 08:16
  • 3
    Ok, but where is the difference between forEach() and forEachRemaining() ? https://pl.kotl.in/ehW6pBa5t – Malachiasz Oct 22 '20 at 04:23
  • 1
    @Malachiasz What part of my answer did you not understand? `forEach` iterates through the elements of an `Iterable`. `forEachRemaining` iterates through the remaining elements of an `Iterator`. – Sweeper Oct 22 '20 at 04:27
  • In my linked sample forEach iterates also through the remaining elements. Or does forEach() in Kotlin I used is not the same as Iterable.forEach() in Java? – Malachiasz Oct 22 '20 at 07:27
  • 1
    @Malachiasz Ah... I didn't understand what you meant by the link. Well, this answer is about Java's `Iterable.forEach` vs `Iterator.forEachRemaining`. I am not an expert in Kotlin. It appears that in Kotlin, `Iterator` also has a `forEach` method that works exactly like `forEachRemaining`. I suggest you ask a new question if you want more accurate info on that. – Sweeper Oct 22 '20 at 07:36
2

In Simple word, forEachRemaining() is an improved version of Iterator, where before JDK8 we were uses hasNext(), next() to traverse the any collection(ArrayList, HastSet etc).

List<String> fruits= new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Grapes");
fruits.add("Orange");
Iterator<String> iterator = fruits.iterator();

Before JDK8

while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

Now In JDK8 if we want to we want to traverse any collection we can do like this:

After JDK8

iterator.forEachRemaining(System.out::println);

Now, JDK8 forEach() is improved version JDK7 with lamda expression.

Mohammed H
  • 6,880
  • 16
  • 81
  • 127
0

Just summarizing for Scala and Kotlin users: in Java, Iterator's forEach is called forEachRemaining (while Iterable has a forEach method).

stackoverflowed
  • 686
  • 8
  • 22
0

Additional comment to @Sweeper's answer:

forEach is for Iterable, which corresponds to a "collection", thus "for each" is "for each element of the collection".

forEachRemaing is for Iterator, which corresponds to a pointer. There may or may not be a "collection" behind the pointer; "for each element of a pointer" is not valid statement.

Fang Zhang
  • 1,597
  • 18
  • 18