Count number of items with property

后端 未结 10 2544
不思量自难忘°
不思量自难忘° 2021-02-13 12:18

I have list List where Custom is like

class Custom{
    public int id;
    public String name;
}

How to

10条回答
  •  说谎
    说谎 (楼主)
    2021-02-13 12:53

    This can now be done easily with Java 8 streams — no extra libraries required.

    List list = /*...*/;
    long numMatches = list.stream()
                          .filter(c -> "Tom".equals(c.name))
                          .count();
    

提交回复
热议问题