比较一个list集合里是否有重复的值(如果有删除重复值)

    /**
     * 比较一个list集合里是否有重复的值(如果有删除重复值)
     * */
    
    //最优法
    public static List<Object> removeRepeat(List<Object> list) {
        Set<Object> set = new HashSet<Object>(list.size());
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    //一般法
    public static List<Object> removeRepeat2(List<Object> list) {
        List<Object> noRepeatList = new ArrayList<Object>();
        for (int i = 0; i < list.size(); i++) {
            boolean isok = true;
            for (int j = i + 1; j < list.size(); j++) {
                if (list.get(i).equals(list.get(j))) {
                    isok = false;
                }
            }
            if (isok) {
                noRepeatList.add(list.get(i));
            }
        }
        return noRepeatList;
    }

 

你可能感兴趣的:(list)