java中使用hashSet的特性,判断数组是否有重复值

 1 public static boolean cheakRepeat(int[] array){
 2   HashSet<Integer> hashSet = new HashSet<Integer>();
 3   for (int i = 0; i < array.length; i++) {
 4     hashSet.add(array[i]);
 5   }
 6   if (hashSet.size() == array.length){
 7     return true;
 8   }else {
 9   return false; 
10   }
11 }

 由于hashset  实现了set接口,所以它不允许集合中有重复的值,在调用add方法时,如果插入了重复值,会返回false。

hashset的更多特性可以看这篇博客http://www.cnblogs.com/chenjfblog/p/7522158.html

原文地址:https://www.cnblogs.com/chenjfblog/p/7522158.html