• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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: 59765
188
  • 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: 59765
188
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!