5.WeakHashMap

(1) 简介

WeakHashMap与HashMap几乎都是相同的,就是它的键是“弱引用”。

第一个问题:何为弱引用?即WeakReference类对象。

String a = new String ("A");   //Strong Reference
WeakReference <String> b = new WeakReference ("B");  //Weak Reference

以上即为强引用和弱引用的典型例子。

第二个:那么他们的区别在哪里?

当弱引用不再被使用时,就会被回收。也就是说,在WeakHashMap中有一个键的引用失效后,将会移除对应的节点。

第三个:这又有什么用呢?

坦率来说,我平常编代码时没用过WeakHashMap。WeakHashMap一般用在作为大量缓存的情况之下,可以有效地节省内存。

(2)WeakHashMap的使用和特点展示

由于WeakHashMap的特点决定了它不会非常地常用,并且大量代码、使用方法、数据结构都与HashMap完全相同,因此不会对它进行过于详细地解读。构造函数就与HashMap的形式相同,成员域多了一个队列,作用是存储已被GC清除”的“弱引用的键”。

        WeakHashMap<String,String> wh = new WeakHashMap();
        HashMap<String,String> hm = new HashMap();
        String a = new String ("A");
        String b = new String("B");
        wh.put(a,"first");
        wh.put(b,"second");
        hm.put(a,"first");
        hm.put(b,"second");

        System.out.println(wh);
        System.out.println(hm);

上述代码仅仅实现了插入两个元素,接下来展示弱引用的特点:

        hm.remove(a);
        a=null;
        b=null;
        System.gc();
        System.out.println(hm);
        System.out.println(wh);

来看一下结果,

集合(六) WeakHashMap与LinkedHashMap

前面三个都没有问题,可是最后一个WeakHashMap怎么也少了一个元素?原因就是这个节点的键——即引用a已经没有指向了,所以也会在WeakHashMap中删除了。可能有人会问引用b不是也赋值为null了吗?为什么不删除,那是因为b还有用!b仍作为HashMap中一个键的引用发挥着作用,因此达不到回收的条件。

 

6.LinkedHashMap

 HashMap是无序的,TreeMap是可以按照自定义的顺序排列,但是LinkedHashMap可以按照插入或者访问的顺序进行遍历,这还是很有实用性的。以下为LinkedHashMap的基本操作:

        LinkedHashMap lh = new LinkedHashMap();
        lh.put(1,"w");
        lh.put(2,"u");
        lh.put(3," ");
        lh.put(4,"y");
        lh.put(5,"i");
        lh.put(6," ");
        lh.put(7,"m");
        lh.put(8,"i");
        lh.put(9,"n");
        lh.put(10,"g");

        Collection<String> c = lh.values();

        for(String s:c)
            System.out.print(s);
wu yi ming

可以看出,只要按照顺序就可以了!

因为LinkedHashMap是继承HashMap实现的,所以特性和HashMap都是相同的,比如说可以有null元素,以及不是线程安全的,当然关于构造函数,除了和HashMap重复的,只有一个构造函数需要单独解释一下.

        /**
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
     * with the specified initial capacity and load factor.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */

public LinkedHashMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
        accessOrder = false;
    }

    /**
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
     * with the specified initial capacity and a default load factor (0.75).
     *
     * @param  initialCapacity the initial capacity
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public LinkedHashMap(int initialCapacity) {
        super(initialCapacity);
        accessOrder = false;
    }

    /**
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
     * with the default initial capacity (16) and load factor (0.75).
     */
    public LinkedHashMap() {
        super();
        accessOrder = false;
    }

    /**
     * Constructs an insertion-ordered <tt>LinkedHashMap</tt> instance with
     * the same mappings as the specified map.  The <tt>LinkedHashMap</tt>
     * instance is created with a default load factor (0.75) and an initial
     * capacity sufficient to hold the mappings in the specified map.
     *
     * @param  m the map whose mappings are to be placed in this map
     * @throws NullPointerException if the specified map is null
     */
    public LinkedHashMap(Map<? extends K, ? extends V> m) {
        super();
        accessOrder = false;
        putMapEntries(m, false);
    }

    /**
     * Constructs an empty <tt>LinkedHashMap</tt> instance with the
     * specified initial capacity, load factor and ordering mode.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @param  accessOrder     the ordering mode - <tt>true</tt> for
     *         access-order, <tt>false</tt> for insertion-order
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
    public LinkedHashMap(int initialCapacity,
                         float loadFactor,
                         boolean accessOrder) {
        super(initialCapacity, loadFactor);
        this.accessOrder = accessOrder;
    }
LinkedHashMap Constructor

相关文章:

  • 2021-07-11
  • 2021-07-09
  • 2021-08-05
  • 2021-10-16
  • 2021-07-05
  • 2022-01-07
  • 2021-12-31
猜你喜欢
  • 2022-01-19
  • 2021-10-29
  • 2022-12-23
  • 2021-04-26
  • 2021-10-24
  • 2021-11-05
相关资源
相似解决方案