Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Which of the following are required to iterate through a collection using the forEach method?  RSS feed

 
Anya Sherwood
Greenhorn
Posts: 5
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a question on Transcender - and I answered it wrong but I think it maybe wrong.  Here is the question

Which of the following are required to iterate through a collection using the forEach method?

lambda expression
stream
collect
map

My reasoning is that since in the question you have a collection and forEach, all that is required is a consumer lamba expression.  And I coded up a sample of each.  And "stream()" technically is not required to go through a "collection".  I used an List/ArrayList to create the collection.  Can anyone explain why the answer would be both lambda and stream?  without stream(), forEach still iterates?
 
Stephan van Hulst
Saloon Keeper
Posts: 7504
135
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with your reasoning. I would have answered lambda expression as well.
 
Roel De Nijs
Sheriff
Posts: 11302
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
Anya Sherwood wrote:My reasoning is that since in the question you have a collection and forEach, all that is required is a consumer lamba expression.

Agreed!

In fact, if you write something like list.stream().forEach(); IntelliJ IDEA gives you a warning that the stream() call is redundant and can be removed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!