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

 public static boolean cheakRepeat(int[] array){
HashSet<Integer> hashSet = new HashSet<Integer>();
for (int i = 0; i < array.length; i++) {
hashSet.add(array[i]);
}
if (hashSet.size() == array.length){
return true;
}else {
return false;
}
}

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

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

上一篇:java.lang.NoClassDefFoundError:


下一篇:搭建Android浏览器壳子