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

Creating a word cloud question

I have tried to do this but seems like im doing it wrongly. Can someone assist on how i can pass this question

package com.teamtreehouse.challenges.highscores;

import com.teamtreehouse.challenges.highscores.model.Score; import com.teamtreehouse.challenges.highscores.service.ScoreService; import com.sun.xml.internal.fastinfoset.util.StringArray; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream;

public class Main {

public static void main(String[] args) { ScoreService service = new ScoreService(); List<Score> scores = service.getAllScores(); System.out.printf("There are %d total scores. %n %n", scores.size()); System.out.println("Imperatively - Word count of all game titles"); Map<String, Long> wordCounts = getWordCountFromGameTitlesImperatively(scores); wordCounts.forEach((key, value) -> System.out.printf("%s occurs %d times %n", key, value)); System.out.println(); System.out.println("Declaratively - Word count of all game titles"); // NOTE: Reassignment wordCounts = getWordCountFromGameTitlesDeclaratively(scores); wordCounts.forEach((key, value) -> System.out.printf("%s occurs %d times %n", key, value)); }

public static Map<String,Long> getWordCountFromGameTitlesImperatively(List<Score> scores) { Map<String, Long> wordByCount = new HashMap<>(); for (Score score : scores) { String title = score.getGame().toLowerCase(); String[] words = title.split("\W+"); for (String word : words) { if (word.isEmpty()) { continue; } long count = wordByCount.getOrDefault(word, 0L); wordByCount.put(word, ++count); } } return wordByCount; }

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

public static List<String> getFirstFiveAmazingPlayersImperatively(List<Score> scores) {
List<String> players = new ArrayList<>();
for (Score score : scores) {
  if (score.getAmount() > 100000) {
    players.add(score.getPlayer());
    if (players.size() >= 5) {
      break;
    }
  }
}
return players;

}

public static List<String> getFirstFiveAmazingPlayersDeclaratively(List<Score> scores) { return scores.stream() .filter(score -> score.getAmount() > 100000) .map(Score::getPlayer) .limit(5) .collect(Collectors.toList()); }

public static List<Score> getFirstThreeNintendoScoresImperatively(List<Score> scores) { List<Score> result = new ArrayList<>(); // Four score and 7 years ago....#dadjoke for (Score score : scores) { if (score.getPlatform().equals("Nintendo Entertainment System")) { result.add(score); if (result.size() >= 3) { break; } } } return result; }

public static List<Score> getFirstThreeNintendoScoresDeclaratively(List<Score> scores) { return scores.stream() .filter(score -> score.getPlatform().equals("Nintendo Entertainment System")) .limit(3) .collect(Collectors.toList()); }

public static void printBurgerTimeScoresImperatively(List<Score> scores) { for (Score score : scores) { if (score.getGame().equals("Burger Time") && score.getAmount() >= 20000) { System.out.println(score); } } }

public static void printBurgerTimeScoresDeclaratively(List<Score> scores) { scores.stream() .filter(score -> score.getGame().equals("Burger Time")) .filter(score -> score.getAmount() >= 20000) .forEach(System.out::println); }

}

1 Answer

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

Hello Genius Mutero

Please see the below code with comments on how I did this challenge declaratively:

public static List<String> getFirstFiveAmazingPlayersDeclaratively(List<Score> scores) {

//filtering greater than 100,000
    return scores.stream().filter(score -> score.getAmount() > 100000)
//mapping Score to Player's Name
                 .map(Score::getPlayer)

//limiting the number of players to 5
                 .limit(5)
//collecting into a List
                 .collect(Collectors.toList());
  }

This was my solution for the word part.

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(String::toLowerCase)
                 .map(word->word.split("\\W+"))
                 .flatMap(Stream::of)
                 .collect(Collectors.groupingBy(word->word, Collectors.counting()));

  }

I hope this helps you, let me know.

Cheers.....