• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you know that IntConsumer's forEach() requires an object that implements IntConsumer method?  RSS feed

 
Alan Ong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning on Lambdas currently from a book and please kindly have a look at the code from it.



My questions are only between lines 14 - 15.
Why is it that IntStream's forEach() requires an object that implements the IntConsumer functional interface in order to perform a task on each stream element?
And then from there it invokes the accept method of IntConsumer.

Thank in advance!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since an IntStream is a stream of integer values, it seems only fitting that its forEach method requires an IntConsumer, don't you think? What else would it be if not?

See this for more about lambda target typing: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#target-typing
 
Alan Ong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Well, since an IntStream is a stream of integer values, it seems only fitting that its forEach method requires an IntConsumer, don't you think? What else would it be if not?

See this for more about lambda target typing: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#target-typing


Thanks for the useful link I will go and practise with the codes starting from the beginning!

Actually one thing which confuses me is that why do need an object as an argument for the method?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Ong wrote:Actually one thing which confuses me is that why do need an object as an argument for the method?

What line of code are you referring to? What object/argument and what method?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know? Well look at the methods of IntStream and scroll down to forEach, it tells you that. That means somebody has written a method like this:-If you look at IntConsumer, you will find it is a FunctionalInterface with one abstract method. You can see which method is which by clicking the all abstract and default tabs. That means the compiler “knows” which method to implement if you write a λ. You can see other examples of functional interfaces, e.g. Runnable, ActionListener, and Comparator. ActionListener isn't marked functional, but it is because it only has one method. Comparator might appear to have 14 methods, but only one is abstract, so it is marked functional. The link about functional interfaces a few lines back is good.

I am duplicating this discussion in our Java8 forum
 
Alan Ong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell thanks for taking the time to explain things to me.

I double-checked and find that the forEach method lies under the abstract method instead of default.

Erm my confusion is that as IntStream and IntConsumer are interfaces, shouldn't we need an object to run those methods?
I mean I learnt that we cannot instantiate interfaces.
Additionally, from this tutorial of the book there isn't any object available that can be used for invoking the methods.
 
Alan Ong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh one more thing I'm confused is that right from the beginning of this tutorial the books says that IntStream method of (line 14 receives an int array as an argument and returns an IntStream for processing the array's values. Once you create a stream, you can chain together multiple method calls to create a stream pipeline.

I do not understand what it means by it "returns an IntStream for processing the arrays' values", does it mean an IntStream object is being returned here?
And also "once you create a stream, you can chain together...", so where is the stream being created?
Thanks in advance!
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erm my confusion is that as IntStream and IntConsumer are interfaces, shouldn't we need an object to run those methods?
I mean I learnt that we cannot instantiate interfaces. 

You are correct that you can't instantiate an interface, but you can use one as a type.  In fact, it's advantageous to use an interface as a type.  For instance: Here the type is List and you use the implementation ArrayList to instantiate it.  This makes it easier to change implementations later.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand what it means by it "returns an IntStream for processing the arrays' values", does it mean an IntStream object is being returned here?
And also "once you create a stream, you can chain together...", so where is the stream being created? 

Let's take a simple example:

The static method "of" returns an object of type IntStream.  The ".forEach()" method takes that object and iterates over it.  It's sometime helpful to think of an object of type IntStream as a "flow" or source of numbers... a stream.  But if that confuses you, it's just an object that can be iterated over.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!