Check whether list of custom objects have same value for a property in Java 8

前端 未结 5 894
逝去的感伤
逝去的感伤 2020-12-16 13:29

I am new to Java 8. I have a list of custom objects of type A, where A is like below:

 class A {
      int id;
      String name;
 }

I woul

5条回答
  •  时光说笑
    2020-12-16 14:01

    One way is to get the name of the first list and call allMatch and check against that.

    String firstName = yourListOfAs.get(0).name;
    boolean allSameName = yourListOfAs.stream().allMatch(x -> x.name.equals(firstName));
    

提交回复
热议问题