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,不允许多条记录的值为null。HashMap不支持线程的同步, * 即任一时刻可以有多个线程同时写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 * Hashtable与HashMap类似,不同的是:它不允许记录的键或者值为空;它支持线程的同步, * 即任一时刻只有一个线程能写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())); } }}
阅读全文
0 0
- JAVA中MAP的四种类型区别和常见的简单用法
- burpsuite中intruder标签内attack type四种类型的用法和区别
- java 中list,set,map集合的用法和区别
- java 中list,set,map集合的用法和区别
- 程序员常见的四种类型
- java中map的种类区别及排序详解(map集合的使用)
- Java中常见的Exception种类
- java中常见的session种类
- map函数,java和C++的常见用法
- Java中信息提示框的四种类型
- Java Map的几种类型
- java中Map常见用法
- 常见的四种类型的J2EE架构
- 简介常见的四种类型的J2EE架构
- Java锁的种类和区别
- android中anumation的四种类型
- java中list,set,map用法的区别
- Java中Set、Map、List的用法与区别
- http://write.blog.csdn.net/postedit
- poj-2182-Lost Cows (树状数组,线段树)
- 《leetcode》best-time-to-buy-and-sell-stock-i-ii-iii
- C/C++中的关键字
- 递归的函数
- JAVA中MAP的四种类型区别和常见的简单用法
- attention and debug
- klass实现js的类的方式
- 翻转链表
- jQuery学习笔记2
- HDU-5155 Harry And Magic Box(DP,组合数)
- 简单的多线程编程_同步与互斥问题
- lightoj 1028
- python中的pop函数和append函数