LinkedHashMap和HashMap的区别以及使用方法
来源:互联网 发布:单片机串口通信原理 编辑:程序博客网 时间:2024/06/08 10:05
package javatest;import org.junit.Test;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;/** * Created by Lovell on 7/16/16. */public class LinkedHashMapTest { @Test public void test() throws Exception { // 顾名思义LinkedHashMap是比HashMap多了一个链表的结构。与HashMap相比LinkedHashMap维护的是一个具有双重链表的HashMap, // LinkedHashMap支持两种排序一种是插入排序,一种是使用排序,最近使用的会移至尾部例如 M1 M2 M3 M4,使用M3后为 M1 M2 M4 M3了, // LinkedHashMap输出时其元素是有顺序的,而HashMap输出时是随机的,如果Map映射比较复杂而又要求高效率的话,最好使用LinkedHashMap, // 但是多线程访问的话可能会造成不同步,所以要用Collections.synchronizedMap来包装一下,从而实现同步。其实现一般为: // Map<String String> map = Collections.synchronizedMap(new LinkedHashMap(<String String)); // LinkedHashmap 的特点是put进去的对象位置未发生变化,而HashMap会发生变化. System.out.println("*************************LinkedHashMap*************"); Map<Integer, String> map = new LinkedHashMap<Integer, String>(); map.put(6, "apple"); map.put(3, "banana"); map.put(2, "pear"); for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { Object key = it.next(); System.out.println(key + "=" + map.get(key)); } System.out.println("*************************HashMap*************"); Map<Integer, String> map1 = new HashMap<Integer, String>(); map1.put(6, "apple"); map1.put(3, "banana"); map1.put(2, "pear"); for (Iterator it = map1.keySet().iterator(); it.hasNext(); ) { Object key = it.next(); System.out.println(key + "=" + map1.get(key)); } }}
0 0
- LinkedHashMap和HashMap的区别以及使用方法
- LinkedHashMap和HashMap的区别以及使用方法
- LinkedHashMap和HashMap的区别以及使用方法
- LinkedHashMap和HashMap的区别以及使用方法
- LinkedHashMap和HashMap的区别以及使用方法
- LinkedHashMap和HashMap的区别以及使用方法
- LinkedHashMap和HashMap的区别以及使用方法
- LinkedHashMap 和 HashMap的区别
- LinkedHashMap 和 HashMap的区别
- LinkedHashMap 和 HashMap的区别
- LinkedHashMap 和 HashMap的区别
- LinkedHashMap 和 HashMap的区别
- LinkedHashMap 和 HashMap的区别
- LinkedHashMap 和 HashMap的区别
- LinkedHashMap 和 HashMap的区别
- LinkedHashMap和HashMap的区别
- HashMap和LinkedHashMap的区别
- HashMap和LinkedHashMap的区别
- 判断用户是安卓还是ios的php代码
- 二叉树的按层遍历
- opencv的一个神经网络例程(转自http://blog.sina.com.cn/s/blog_61e10f020101bv94.html)
- TCP组包
- 给EventBus增加订阅收到消息提醒,用AndroidStudio重新打包成Jar文件
- LinkedHashMap和HashMap的区别以及使用方法
- for 语句
- Android Studio中“非法字符\\65279,Error:(1, 10) 错误: 需要class, interface或enum”
- Part3:关联容器(二)&Part4:迭代器
- UVALive 3026 period(周期) kmp算法的应用
- Java技术内幕
- 2016.07.16【初中部 NOIP提高组 】模拟赛C总结
- 杭电OJ1008-电梯Elevator
- 树状数组点修改+段求值