Check if all values in a map are equal

后端 未结 6 1185
一生所求
一生所求 2021-01-15 03:37

I need to check if all values in a map are equal. I have a method to perform this task but would like to use a library or native methods. Limitations: Java 5 + Apache Common

6条回答
  •  被撕碎了的回忆
    2021-01-15 04:09

    As my comment above:

    //think in a more proper name isAllValuesAreUnique for example
        public static boolean isUnique(Map aMap){
             if(aMap == null)
                   return true; // or throw IlegalArgumentException()
    
             Collection c = aMap.getValues(); 
             return new HashSet<>(c).size() <= 1;
        }
    

提交回复
热议问题