zoukankan      html  css  js  c++  java
  • HashMap、LinkedMap和TreeMap的比较

    目的

    主要想测试一下HashMap、LinkedHashMap和TreeMap的有序性

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.TreeMap;
    
    /**
     * 三种map是否有序测试:HashMap,LinkedMap,TreeMap
     * 
     * @author Lynn
     *
     */
    public class TestMap {
    	public static void main(String[] args) {
    		Map<String,String> hashMap = new HashMap<String,String>();
    		Map<String,String> linkedHashMap = new LinkedHashMap<String,String>();
    		Map<String,String> treeMap = new TreeMap<String,String>();
    		
    		//添加相同的数据;
    		hashMap.put("A", "A");
    		hashMap.put("B", "B");
    		hashMap.put("1", "1");
    		hashMap.put("2", "2");
    		
    		linkedHashMap.put("A", "A");
    		linkedHashMap.put("B", "B");
    		linkedHashMap.put("1", "1");
    		linkedHashMap.put("2", "2");
    		
    		treeMap.put("A", "A");
    		treeMap.put("B", "B");
    		treeMap.put("1", "1");
    		treeMap.put("2", "2");
    		
    		
    		Iterator iterator = hashMap.entrySet().iterator();
    		System.out.println("HashMap的输出结果:");
    		while(iterator.hasNext()) {
    		Map.Entry<String, String> entity = (Entry<String, String>) iterator.next();
    			System.out.println("[ key="+entity.getKey()+" value="+entity.getValue()+"]");
    		}
    		
    		iterator = linkedHashMap.entrySet().iterator();
    		System.out.println("LinkedMap的输出结果:");
    		while(iterator.hasNext()) {
    		Map.Entry<String, String> entity = (Entry<String, String>) iterator.next();
    			System.out.println("[ key="+entity.getKey()+" value="+entity.getValue()+"]");
    		}
    		
    		iterator = treeMap.entrySet().iterator();
    		System.out.println("TreeMap的输出结果:");
    		while(iterator.hasNext()) {
    		Map.Entry<String, String> entity = (Entry<String, String>) iterator.next();
    			System.out.println("[ key="+entity.getKey()+" value="+entity.getValue()+"]");
    		}
    	}
    	
    }
    
    

    运行结果:

    结论

    共同点:
    HashMap,LinkedHashMap,TreeMap都属于Map;Map 主要用于存储键(key)值(value)对,根据键得到值,因此键不允许键重复,但允许值重复。
    不同点:

    1. HashMap里面存入的键值对在取出的时候是随机的,也是我们最常用的一个Map.它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。在Map 中插入、删除和定位元素,HashMap 是最好的选择。
    2. TreeMap取出来的是排序后的键值对。但如果您要按自然顺序(字典顺序),那么TreeMap会更好。
    3. LinkedHashMap 是HashMap的一个子类,如果需要输出的顺序和输入的相同,那么用LinkedHashMap可以实现。

    参考
    https://blog.csdn.net/u012889434/article/details/48055679/

    多思考,多尝试。
  • 相关阅读:
    周末之个人杂想(十三)
    PowerTip of the DaySorting Multiple Properties
    PowerTip of the DayCreate Remoting Solutions
    PowerTip of the DayAdd Help to Your Functions
    PowerTip of the DayAcessing Function Parameters by Type
    PowerTip of the DayReplace Text in Files
    PowerTip of the DayAdding Extra Information
    PowerTip of the DayPrinting Results
    Win7下IIS 7.5配置SSAS(2008)远程访问
    PowerTip of the DayOpening Current Folder in Explorer
  • 原文地址:https://www.cnblogs.com/LynnMin/p/9645468.html
Copyright © 2011-2022 走看看