JAVA中MAP的四种类型区别和常见的简单用法

来源:互联网 发布:英剧 知乎 编辑:程序博客网 时间:2024/06/14 23:53

package com.springtest.map;import org.junit.Test;import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * HashMapA * HashMap是一个最常用的Map,它根据键的hashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。 * HashMap最多只允许一条记录的键为null,不允许多条记录的值为nullHashMap不支持线程的同步, * 即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。如果需要同步, * 可以用Collections.synchronizedMap(HashMapA map)方法使HashMap具有同步的能力。 * Created by Joker on 2017/8/24. */public class HashMapA {    private Map<String, String> map = new java.util.HashMap<>();    @Test    public void test() throws InterruptedException {        //HashMap多线程,线程不安全,此处使用Collections.synchronizedMap使HashMap具有同步能力        Collections.synchronizedMap(map);        ExecutorService executorService = Executors.newCachedThreadPool();        executorService.submit(() -> map.put(null, null));        executorService.submit(() -> map.put("1", "A"));        executorService.submit(() -> map.put("2", "B"));        Thread.sleep(1000);        for (Map.Entry<String, String> key : map.entrySet()) {            System.out.println(String.format("[key]=%s\t[value]=%s", key.getKey(), key.getValue()));        }    }}

package com.springtest.map;import org.junit.Test;import java.util.Hashtable;import java.util.Iterator;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * HashTableA * HashtableHashMap类似,不同的是:它不允许记录的键或者值为空;它支持线程的同步, * 即任一时刻只有一个线程能写Hashtable,然而,这也导致了Hashtable在写入时会比较慢 * Created by Joker on 2017/8/24. */public class HashTableA {    @Test    public void test() throws InterruptedException {        //Hashtable本身就是同步机制,所以比较慢        Map<String, Object> map = new Hashtable<>();        ExecutorService executorService = Executors.newCachedThreadPool();        //executorService.execute(() -> map.put(null, null));        executorService.execute(() -> map.put("1", "A"));        executorService.execute(() -> map.put("2", "B"));        Thread.sleep(1000);        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();        while (it.hasNext()) {            Map.Entry<String, Object> keyValue = it.next();            System.out.println(String.format("[key]=%s\t[value]=%s", keyValue.getKey(), keyValue.getValue()));        }    }}

package com.springtest.map;import org.junit.Test;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * LinkedHashMapA * LinkedHashMap保存了记录的插入顺序,在用Iteraor遍历LinkedHashMap时, * 先得到的记录肯定是先插入的。在遍历的时候会比HashMap慢。有HashMap的全部特性。 * Created by Joker on 2017/8/24. */public class LinkedHashMapA {    @Test    public void test() throws InterruptedException {        //多线程,线程不安全        Map<String, Object> map = new LinkedHashMap<>();        ExecutorService executorService = Executors.newCachedThreadPool();        executorService.execute(() -> {            map.put(null, null);            map.put("1", "A");            map.put("2", "B");            map.put("3", "C");            map.put("4", "D");            map.put("5", "E");        });        Thread.sleep(1000);        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();        while (it.hasNext()) {            Map.Entry<String, Object> keyValue = it.next();            System.out.println(String.format("[key]=%s\t[value]=%s", keyValue.getKey(), keyValue.getValue()));        }    }}

package com.springtest.map;import org.junit.Test;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import java.util.TreeMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * TreeMapA * TreeMap能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器。 * 当用Iteraor遍历TreeMap时,得到的记录是排过序的。TreeMap的键和值都不能为空。 * TreeMap取出来的是排序后的键值对。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。 * Created by Joker on 2017/8/24. */public class TreeMapA {    @Test    public void test() throws InterruptedException {        //多线程,线程不安全        Map<String, Object> map = new TreeMap<>();        ExecutorService executorService = Executors.newCachedThreadPool();        executorService.execute(() -> {            //map.put(null, null);            map.put("4", "D");            map.put("2", "B");            map.put("3", "C");            map.put("5", "E");            map.put("1", "A");        });        Thread.sleep(1000);        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();        while (it.hasNext()) {            Map.Entry<String, Object> keyValue = it.next();            System.out.println(String.format("[key]=%s\t[value]=%s", keyValue.getKey(), keyValue.getValue()));        }    }}


原创粉丝点击