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

collecting 3 nes scores

package com.teamtreehouse.challenges.highscores;

import com.teamtreehouse.challenges.highscores.model.Score; import com.teamtreehouse.challenges.highscores.service.ScoreService; import sun.applet.AppletListener;

import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors;

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 - First 3 'Nintendo Entertainment System' Scores"); List<Score> nintendoScores = getFirstThreeNintendoScoresImperatively(scores); nintendoScores.forEach(System.out::println); System.out.println("Declaratively - First 3 'Nintendo Entertainment System' Scores"); // NOTE: Reassigning here nintendoScores = getFirstThreeNintendoScoresDeclaratively(scores); nintendoScores.forEach(System.out::println); }

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) { // TODO: Filter the scores stream to be of platform 'Nintendo Entertainment System' // TODO: Limit it to 3 scores // TODO: Return a newly collected list using the collect method return scores.stream() .filter() .limit(3) .collect(Collectors.toList());

return Collections.emptyList();

}

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); }

}