比较一个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;
    }

猜你喜欢

转载自fengzhaoyang.iteye.com/blog/2261313