java 判断list 集合数据是否存在重复值

2019-04-09  本文已影响0人  囝囝123
/**
     * 检查身份证是否重复
     * @param list
     * @return
     */
    public Boolean checkIdCard(List<String> list){
        
        Set<String> set=new HashSet<String>(list);
        boolean  result= list.size() == set.size() ? true : false; 
        
        return result;
    }
    
    
    public String getRepeatIdCard(List<String> list){
        StringBuffer buffer = new StringBuffer();
        buffer.append("身份证重复值是:");
        String temp = "";
        for (int i = 0; i < list.size() - 1; i++) {
            temp = list.get(i);
            for (int j = i + 1; j < list.size(); j++)
            {
                if (temp.equals(list.get(j)))
                {
                    System.out.println("第" + (i + 1) + "个跟第" + (j + 1) + "个重复,值是:" + temp);
                    buffer.append(temp+",");
                }
            }
        }
        return buffer.toString();
        
    }
上一篇 下一篇

猜你喜欢

热点阅读