TreeMap

TreeMap是有序map,通过key进行排序

1.TreeMap是如何实现去重和排序的?

TreeMap实现了SortedMap接口,它是一个key有序的Map类。

TreeMap的默认排序规则:根据key元素的 compareTo 方法来排序。

2.自定义排序:

我们还可以向TreeMap的构造方法中传入 Comparator 对象来覆盖元素的默认排序规则。即:当TreeMap 创建时如果有传入 Comparator ,优先按照 Comparator 的规则来排序,如果没有传入 Comparator ,就按照key的 compareTo 方法来排序。

判断重复的依据:

在HashMap中判断key是否重复的依据是根据hash值和equals比较,但是在TreeMap中,判断key是否重复的依据是根据 comparaTo 是否为0,如果为0,TreeMap 就认为key是重复的。

从源码上看TreeMap去重和排序的实现原理:

在添加时,key去重的原理:

从TreeMap的put方法源代码每次要向TreeMap中添加新的键值对时,会执行以下步骤:

定义一个指针先指向二叉树的根节点
将传入的key与指针节点的key对比,如果传入的key大于指针节点的key,就将指针移动到当前节点的右子树,如果传入的key小于指针节点的key,就将指针移动到当前节点的左子树。
循环执行上一步,直到传入的key值与指针节点的key值相等,就将value值替换掉,或者指针已经移动到底了,就将传入的key和value作为新的节点,添加到树的底部
为了维护红黑树的平衡,将一些节点翻转,代码细节在 fixAfterInsertion 方法中,这里面的代码与插入去重无关,就不再展开来说了。
在遍历的时候时如何有序的:

由于TreeMap在存入节点的时候已经是按照排序二叉树的特性来插入了,所以在遍历二叉树的时候只要用中序遍历,就可以按照从小到大的顺序读取每一个节点了。

    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     *
     * @return the previous value associated with {@code key}, or
     *         {@code null} if there was no mapping for {@code key}.
     *         (A {@code null} return can also indicate that the map
     *         previously associated {@code null} with {@code key}.)
     * @throws ClassCastException if the specified key cannot be compared
     *         with the keys currently in the map
     * @throws NullPointerException if the specified key is null
     *         and this map uses natural ordering, or its comparator
     *         does not permit null keys
     */
    public V put(K key, V value) {
        TreeMapEntry<K,V> t = root;
        if (t == null) {
            compare(key, key); // type (and possibly null) check

            root = new TreeMapEntry<>(key, value, null);
            size = 1;
            modCount++;
            return null;
        }
        int cmp;
        TreeMapEntry<K,V> parent;
        // split comparator and comparable paths
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            do {
                parent = t;
                cmp = cpr.compare(key, t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        else {
            if (key == null)
                throw new NullPointerException();
            @SuppressWarnings("unchecked")
                Comparable<? super K> k = (Comparable<? super K>) key;
            do {
                parent = t;
                cmp = k.compareTo(t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        TreeMapEntry<K,V> e = new TreeMapEntry<>(key, value, parent);
        if (cmp < 0)
            parent.left = e;
        else
            parent.right = e;
        fixAfterInsertion(e);
        size++;
        modCount++;
        return null;
    }

1.HashMap、Hashtable不是有序的;

2.TreeMap和LinkedHashMap是有序的(TreeMap默认 Key 升序,LinkedHashMap则记录了插入顺序)。

 

   HashMap:我们最常用的Map,它根据key的HashCode 值来存储数据,根据key可以直接获取它的Value,同时它具有很快的访问速度。HashMap最多只允许一条记录的key值为Null(多条会覆盖);允许多条记录的Value为 Null。非同步的。

      TreeMap: 能够把它保存的记录根据key排序,默认是按升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。TreeMap不允许key的值为null。非同步的。

      Hashtable: 与 HashMap类似,不同的是:key和value的值均不允许为null;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢。

      LinkedHashMap: 保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.在遍历的时候会比HashMap慢。key和value均允许为空,非同步的。

 

二、Map排序

TreeMap

      TreeMap默认是升序的,如果我们需要改变排序方式,则需要使用比较器:Comparator。

      Comparator可以对集合对象或者数组进行排序的比较器接口,实现该接口的public compare(T o1,To2)方法即可实现排序,该方法主要是根据第一个参数o1,小于、等于或者大于o2分别返回负整数、0或者正整数。如下:

复制代码
public class TreeMapTest {
    public static void main(String[] args) {
        Map<String, String> map = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj2.compareTo(obj1);
                    }
                });
        map.put("c", "ccccc");
        map.put("a", "aaaaa");
        map.put("b", "bbbbb");
        map.put("d", "ddddd");
        
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            System.out.println(key + ":" + map.get(key));
        }
    }
}
复制代码

  运行结果如下:

      d:ddddd 
      c:ccccc 
      b:bbbbb 
      a:aaaaa

      上面例子是对根据TreeMap的key值来进行排序的,但是有时我们需要根据TreeMap的value来进行排序。对value排序我们就需要借助于Collections的sort(List<T> list, Comparator<? super T> c)方法,该方法根据指定比较器产生的顺序对指定列表进行排序。但是有一个前提条件,那就是所有的元素都必须能够根据所提供的比较器来进行比较。如下:

复制代码
public class TreeMapTest {
    public static void main(String[] args) {
        Map<String, String> map = new TreeMap<String, String>();
        map.put("d", "ddddd");
        map.put("b", "bbbbb");
        map.put("a", "aaaaa");
        map.put("c", "ccccc");
        
        //这里将map.entrySet()转换成list
        List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet());
        //然后通过比较器来实现排序
        Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
            //升序排序
            public int compare(Entry<String, String> o1,
                    Entry<String, String> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
            
        });
        
        for(Map.Entry<String,String> mapping:list){ 
               System.out.println(mapping.getKey()+":"+mapping.getValue()); 
          } 
    }
}
复制代码

运行结果

      a:aaaaa 
      b:bbbbb 
      c:ccccc 
      d:ddddd

 

HashMap

      我们都是HashMap的值是没有顺序的,他是按照key的HashCode来实现的。对于这个无序的HashMap我们要怎么来实现排序呢?参照TreeMap的value排序,我们一样的也可以实现HashMap的排序。

复制代码
public class HashMapTest {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("c", "ccccc");
        map.put("a", "aaaaa");
        map.put("b", "bbbbb");
        map.put("d", "ddddd");
        
        List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
            //升序排序
            public int compare(Entry<String, String> o1,
                    Entry<String, String> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
            
        });
        
        for(Map.Entry<String,String> mapping:list){ 
               System.out.println(mapping.getKey()+":"+mapping.getValue()); 
          } 
     }
}
复制代码

 运行结果

      a:aaaaa 
      b:bbbbb 
      c:ccccc 
      d:ddddd

 

Map的有序和无序实现类,与Map的排序 - 龙昊雪 - 博客园 (cnblogs.com)

posted @ 2023-03-18 15:09  xiaowang_lj  阅读(29)  评论(0编辑  收藏  举报