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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM