Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 58829
179
  • 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: 58829
179
  • 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!