zoukankan      html  css  js  c++  java
  • LinkedHashMap简明

    LinkedHashMap

    构造方法摘要

    inkedHashMap()

    构造一个带默认初始容量 (16) 和加载因子 (0.75) 的空插入顺序 LinkedHashMap 实例。

    LinkedHashMap(int initialCapacity)

    构造一个带指定初始容量和默认加载因子 (0.75) 的空插入顺序 LinkedHashMap 实例。

    LinkedHashMap(int initialCapacity, float loadFactor)

    构造一个带指定初始容量和加载因子的空插入顺序 ·LinkedHashMap· 实例。

    LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)

    构造一个带指定初始容量、加载因子和排序模式的空 LinkedHashMap 实例。 accessOreder : false(默认)为插入顺序, true 为访问顺序。

    LinkedHashMap(Map<? extends K,? extends V> m)

    构造一个映射关系与指定映射相同的插入顺序 LinkedHashMap 实例。


    tips

    如果调用LinkedHashMap(initialCapacity, loadFactor, true),每次用get或put时,受影响的条目将从当前位置删除,并放到链表的尾部(散列表中的桶不会受影响,一个条目总位于其散列码对应的桶中)。

    public class LinkedHashMapT {
    
    	public static void main(String[] args) {
    		LinkedHashMap<String, String> lhMap = new LinkedHashMap<>(16, 0.75F, true);
    		lhMap.put("111", "111");
    		lhMap.put("222", "222");
    		lhMap.put("333", "333");
    		lhMap.put("444", "444");
    		lhMap.put("555", "555");
    		lhMap.put("666", "666");
    		loopLinkedHashMap(lhMap);
    		loopLinkedHashMap(lhMap);
    		loopLinkedHashMap(lhMap);
    		lhMap.get("333");
    		loopLinkedHashMap(lhMap);
    		lhMap.put("222", "222");
    		loopLinkedHashMap(lhMap);
    		lhMap.put("777", "777");
    		loopLinkedHashMap(lhMap);
    	
    	}
    	public static void loopLinkedHashMap(LinkedHashMap<String, String> linkedhashMap){
    		Set<Map.Entry<String,String>> set = linkedhashMap.entrySet();
    		Iterator<Map.Entry<String, String>> it = set.iterator();
    		while(it.hasNext()){
    			System.out.print(it.next() + "\t");
    		}
    		System.out.println();
    	}
    }
    
    111=111	222=222	333=333	444=444	555=555	666=666	
    111=111	222=222	333=333	444=444	555=555	666=666	
    111=111	222=222	333=333	444=444	555=555	666=666	
    111=111	222=222	444=444	555=555	666=666	333=333	
    111=111	444=444	555=555	666=666	333=333	222=222	
    111=111	444=444	555=555	666=666	333=333	222=222	777=777	
    

    LRUCache

    • 构造LinkedHashMap的子类
    • 覆盖方法:protected boolean removeEldestEntry(java.util.Map.Entry<K,V> eldest)
    public class LRUCache extends LinkedHashMap
    {
    	private static final long serialVersionUID = 1L;
        protected int maxElements;
        public LRUCache(int maxSize)
        {
            super(maxSize, 0.75F, true);
            maxElements = maxSize;
        }
    
        protected boolean removeEldestEntry(java.util.Map.Entry eldest)
        {
            return size() > maxElements;
        }
    }
    
  • 相关阅读:
    整理之刷过的数据结构与算法题
    EM 算法求解高斯混合模型python实现
    C语言实现二叉树的基本操作
    scala 小结(一)
    Eclipse 配置运行Spark
    js替换字符串
    安装uni-ui
    判断当前时间的 时分 是否在一个时间段内
    vue 定时器实时刷新数据

  • 原文地址:https://www.cnblogs.com/LittleTreasureBox/p/8799362.html
Copyright © 2011-2022 走看看