List去重(使用TreeSet)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helloworlddm/article/details/82392005

TreeSet:A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
去重代码如下:

//去重的高效算法
    private static   ArrayList<Map<String, Object>>  removeDuplicate(List<Map<String, Object>> ls) {

                Set<Map<String, Object>> ts = new TreeSet<Map<String, Object>>(new Comparator<Map<String, Object>>(){

                    @Override
                    public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                        if (((String)o1.get("sourceNumber")).compareTo((String)o2.get("sourceNumber")) == 0){
                            //o1.put("cateType",(String)o1.get("cateType")+(String)o2.get("cateType"));

                        }
                        return ((String)o1.get("sourceNumber")).compareTo((String)o2.get("sourceNumber"));
                    }


                });
                ts.addAll(ls);
                return new ArrayList<Map<String, Object> >(ts);
    }

TreeMap使用的底层使用的是红黑树

猜你喜欢

转载自blog.csdn.net/helloworlddm/article/details/82392005