集合(十四)LinkedHashMap详解

我们设计的代码如下所示:
 

package com.rgf.jihe;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * LinkedHashMap的底层实现原理(了解)
 * 源码如下所示:
 * public class LinkedHashMap<K,V>
 *     extends HashMap<K,V>
 *     implements Map<K,V>
 *         父类为HashMap.我们进行继承父类
 *  public LinkedHashMap() {
 *         super();
 *         accessOrder = false;
 *     }
 *     调用的是父类的put方法。
 *     public V put(K key, V value) {
 *         return putVal(hash(key), key, value, false, true);
 *     }
 *     同时putVal也是调用的是HashMap的方法,
 *      final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
 *                    boolean evict) {
 *         Node<K,V>[] tab; Node<K,V> p; int n, i;
 *         if ((tab = table) == null || (n = tab.length) == 0)
 *             n = (tab = resize()).length;
 *         if ((p = tab[i = (n - 1) & hash]) == null)
 *             tab[i] = newNode(hash, key, value, null);
 *         else {
 *             Node<K,V> e; K k;
 *             if (p.hash == hash &&
 *                 ((k = p.key) == key || (key != null && key.equals(k))))
 *                 e = p;
 *             else if (p instanceof TreeNode)
 *                 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
 *             else {
 *                 for (int binCount = 0; ; ++binCount) {
 *                     if ((e = p.next) == null) {
 *                         p.next = newNode(hash, key, value, null);
 *                         if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
 *                             treeifyBin(tab, hash);
 *                         break;
 *                     }
 *                     if (e.hash == hash &&
 *                         ((k = e.key) == key || (key != null && key.equals(k))))
 *                         break;
 *                     p = e;
 *                 }
 *             }
 *             if (e != null) { // existing mapping for key
 *                 V oldValue = e.value;
 *                 if (!onlyIfAbsent || oldValue == null)
 *                     e.value = value;
 *                 afterNodeAccess(e);
 *                 return oldValue;
 *             }
 *         }
 *         ++modCount;
 *         if (++size > threshold)
 *             resize();
 *         afterNodeInsertion(evict);
 *         return null;
 *     }
 *     里面的newNode为LinkedHashMap,重写了该方法:
 *     Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
 *         LinkedHashMap.Entry<K,V> p =
 *             new LinkedHashMap.Entry<K,V>(hash, key, value, e);
 *         linkNodeLast(p);
 *         return p;
 *     }
 *     Entry的底层如下所示: (LinkedHashMap中的内部类:Entry)
 *      static class Entry<K,V> extends HashMap.Node<K,V> {
 *         Entry<K,V> before, after; //能够记录添加的元素的先后顺序
 *         Entry(int hash, K key, V value, Node<K,V> next) {
 *             super(hash, key, value, next);
 *         }
 *     }
 *     而Node的底层如下所示;(HashMap中的内部类:Node)
 *      static class Node<K,V> implements Map.Entry<K,V> {
 *         final int hash;
 *         final K key;
 *         V value;
 *         Node<K,V> next;
 *
 *         Node(int hash, K key, V value, Node<K,V> next) {
 *             this.hash = hash;
 *             this.key = key;
 *             this.value = value;
 *             this.next = next;
 *         }
 *HashSet的底层是HashMap,所存储的值相当于key-value里面的key.
 *
 */
public class LinkedHashMapTest {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put(123,"AA");
        map.put(345,"BB");
        map.put(12,"CC");
        System.out.println(map);

        LinkedHashMap l = new LinkedHashMap();
        l.put(123,"AA");
        l.put(345,"BB");
        l.put(12,"CC");
        System.out.println(l);
    }
}

运行之后如下所示:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直再追梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值