Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Daniel Silva
Daniel Silva
11,209 Points

For this challenge you will modify the method Main.getWordCountFromGameTitlesDeclaratively to to produce a word count of

I'm having trouble solving the following code challenge.

For this challenge you will modify the method Main.getWordCountFromGameTitlesDeclaratively to to produce a word count of all games in the system. You will need to use a flatMap because there will be a stream of streams!

public static Map<String,Long> getWordCountFromGameTitlesDeclaratively(List<Score> scores) {
    // TODO: Open a stream on scores
    // TODO: Map the stream to the game title
    // TODO: Map that to it's lowercase version, so things are case insensitive
    // TODO: Map that to a new String array of each word in the title (See imperative implementation)
    // TODO: Flatten those words into the stream
    // TODO: Collect a grouping of the word to count
    return scores.stream()
      .map(Score::getGame)
      .map(title -> title.split("\\W+"))
      .flatMap(scores -> Stream.of(scores))
      .collect(Collectors.groupingBy(
        Function.identity(),
        Collectors.counting()
      ));
  }

1 Answer

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Dear Daniel Silva

Checked your code and you're doing a fantastic job. The only thing you missed was to convert to LowerCase.

Please see my code below.

return scores.stream()
             .map(Score::getGame)
             .map(String::toLowerCase)
             .map(word->word.split("\\W+"))
             .flatMap(Stream::of)
             .collect(Collectors.groupingBy(word->word, Collectors.counting()));

Thanks,

Tonnie