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

前端 未结 5 907
逝去的感伤
逝去的感伤 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 13:54

    another way is to calculate count of distinct names using

    boolean result = myList.stream().map(A::getName).distinct().count() == 1;
    

    of course you need to add getter for 'name' field

提交回复
热议问题