filter a stream using collect() instead of filter()

filter a stream using collect() instead of filter()

I have the list that contains data that looks like this

I have the list that contains data that looks like this

game_id | user_id | status
   1         1       STARTED
   2         1       FINISHED
   1         2       STARTED
   2         2       FINISHED

I want to collect this list into two maps.

One map should keep mapping of user_id to number of the games that user played - basically games with finished statuses

user_id | finished_games
   1            1
   2            1

Another map should store user_id to all games count. It would look like this

user_id  | all_games
   1            2
   2            2

I do it like this (I removed game_id field as it is that not important in fact)

import java.util.*;
import java.util.stream.*;

public class MyClass { public static void main(String args[]) { List<Game> games = Arrays.asList( create("user-1", "STARTED"), create("user-2", "STARTED"), create("user-1", "FINISHED"), create("user-2", "FINISHED"), create("user-1", "FINISHED"), create("user-2", "FINISHED") );

    // expect user-1: all_games (3), finished_games (2)

    Map&lt;String, Long&gt; allGames = games
        .stream()
        .collect(Collectors.groupingBy(Game::getUserId, Collectors.counting()));
    System.out.println(allGames);

    Map&lt;String, Long&gt; finishedGames = games
        .stream()
        .filter(game -&gt; game.battleStatus.equals("FINISHED"))
        .collect(Collectors.groupingBy(Game::getUserId, Collectors.counting()));
    System.out.println(finishedGames);

}

private static Game create(String id, String status) {
    Game game = new Game();
    game.userId = id;
    game.battleStatus = status;
    return game;
}

private static class Game {
    String userId;
    String battleStatus;

    public String getUserId() {
        return userId;
    }
}

}

It seems to work fine. But when I calculate games with status, I filter in order to leave only items with FINISHED status.

Map<String, Long> finishedGames = games
    .stream()
    .filter(game -> game.battleStatus.equals("FINISHED"))
    .collect(Collectors.groupingBy(Game::getUserId, Collectors.counting()));

Is there a way to achieve that inside collect block instead of using filter?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

Fundamentos de Java: Aprende Java desde cero, sin misterios | Simpliv

Fundamentos de Java: Aprende Java desde cero, sin misterios