How can I count the number of elements that match a predicate with Streams?

后端 未结 1 771
抹茶落季
抹茶落季 2021-01-01 11:48

In Java7 I have this code:

public int getPlayersOnline() {
    int count = 0;
    for (Player player : players) {
        if (player.isActive()) {
                   


        
相关标签:
1条回答
  • 2021-01-01 12:38

    This would be a one-liner:

    return (int) players.stream().filter(Player::isActive).count();
    
    0 讨论(0)
提交回复
热议问题