How can i transform my code using java 8 stream API?

Mangos :

I am writing a simple method to print out statistics of a series of outcomes of games. Every Game has a list of Outcomes in it, which contain enums according to the outcome of a game. My instructor has commented a TODO in my code:

public static void printStatistics(List<Game> games) {
    float win = 0;
    float lose = 0;
    float draw = 0;
    float all = 0;

    //TODO: should be implemented /w stream API
    for (Game g : games) {
        for (Outcome o : g.getOutcomes()) {
            if (o.equals(Outcome.WIN)) {
                win++;
                all++;
            } else if (o.equals(Outcome.LOSE)) {
                lose++;
                all++;
            } else {
                draw++;
                all++;
            }
        }
    }
    DecimalFormat statFormat = new DecimalFormat("##.##");

    System.out.println("Statistics: The team won: " + statFormat.format(win * 100 / all) + " %, lost " + statFormat.format(lose * 100 / all)
            + " %, draw: " + statFormat.format(draw * 100 / all) + " %");

}

I am familiar with lambda expressions. I tried looking online for solutions, but could not find examples of a stream accessing fields of a field of a class. I would be happy if you could give me a solution, or provide me with a relevant tutorial. Thanks.

Ruslan :

groupingBy fits this case:

Map<Outcome, Long> map = games.stream()
        .flatMap(game -> game.getOutcomes().stream())
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

And get win, lose count:

long win = map.get(Outcomes.WIN);
long lose = map.get(Outcomes.LOSE);
...

To get all count you need to sum all values from map

long all = map.values().stream()
    .mapToLong(Long::valueOf)
    .sum();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=87442&siteId=1