Java集合框架学习(十) LinkedHashMap详解

LinkedHashMap介绍


1.Key和Value都允许null;

2.维护key的插入顺序;

3.非线程安全;

4.Key重复会覆盖、Value允许重复。



类定义


public class LinkedHashMap<K,V>

extends HashMap<K,V>

implements Map<K,V>



例子介绍


package com.dylan.collection;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author xusucheng
 * @create 2018-02-03
 **/
public class LinkedHashMapExample {
    public static void main(String args[]) {
        // HashMap Declaration
        LinkedHashMap<Integer, String> lhmap =
                new LinkedHashMap<Integer, String>();

        //Adding elements to LinkedHashMap
        lhmap.put(22, "Abey");
        lhmap.put(33, "Dawn");
        lhmap.put(1, "Sherry");
        lhmap.put(2, "Karon");
        lhmap.put(100, "Jim");

        // Generating a Set of entries
        Set set = lhmap.entrySet();

        // Displaying elements of LinkedHashMap
        Iterator iterator = set.iterator();
        while(iterator.hasNext()) {
            Map.Entry me = (Map.Entry)iterator.next();
            System.out.print("Key is: "+ me.getKey() +
                    "& Value is: "+me.getValue()+"\n");
        }
    }
}








posted @ 2018-02-03 11:10  一锤子技术员  阅读(2)  评论(0编辑  收藏  举报  来源