Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stream.iterate() and groupingBy()  RSS feed

 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there;)

My Last question before the exam;)

I am currently reviewing every Possibility that I
could encounter in the exam, and so I stumbled
upon those two:

Why does this return 11111 and not 12345?

Stream.iterate(1, x -> x++).forEach()System.out::println);

why do I need the the pre-Increment Operator here?

The next one is:

Why does



work with this:
??

I thought only partitioningBy() would take a Predicate and
groupingBy() would take a Function?

Kind regards
Florian
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Florian Jedamzik wrote:Why does this return 11111 and not 12345?

In short: because the post-increment operator will evaluate first before incrementing the actual value. Meaning the expression x -> x++ is functionally equivalent withand the expression return integer++; is functionally equivalent withSo you could either use x -> ++x or x -> x+1 to get the output you were expecting (12345).
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Florian Jedamzik wrote:I thought only partitioningBy() would take a Predicate and
groupingBy() would take a Function?

You have described the API perfectly!
You have defined a <String, Boolean> function, so you'll get exactly the same return type as with the partitioningBy method because the return type of your function (K) is boolean.
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, now I see;)

Thank you Roel, that really helped me a lot!;)

 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best of luck with your exam!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!