• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How can I iterate through this using streams??  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can someone please tell me how can I iterate using streams so that the output will print me all the titles



Thank you in advance!
 
Marshal
Posts: 61690
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What are you trying to iterate? Are you trying to iterate the values set of that Map? I presume you already know how to get the values from the Map as a Collection view.Since all Collections have a stream() method, you can use that to get a Stream from that Collection:-Now, to convert that to a Stream<String> rather than a Stream of Lists, you will need to flatten it, and that flattening method needs a Function to convert the elements to something which can be flattened, probably another Stream. You can probably do that with a method reference, since all you are doing is calling the stream() method on each List.Now let's print that. There are all sorts of options; I shall go for a Collector. How about the Collector which uses a StringJoiner behind the scenes? Method link.Now put the whole lot together into one statement:-I think that will work; I am not quite sure about the flatMap line.

Depending on which book you have read, you may consider my indentation correct or incorrect.
 
Campbell Ritchie
Marshal
Posts: 61690
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . I think that will work. . . .

I missed out a ) which you will have to correct.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!