map的遍历,HashMap,LinkedHashMap,TreeMap的区别
来源:互联网 发布:流程引擎 java 编辑:程序博客网 时间:2024/06/04 00:23
转载:http://liangwang985.blog.163.com/blog/static/11954923320114252810299/
顾名思义LinkedHashMap是比HashMap多了一个链表的结构。与HashMap相比LinkedHashMap维护的是一个具有双重 链表的HashMap,LinkedHashMap支持2中排序一种是插入排序,一种是使用排序,最近使用的会移至尾部例如 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));
HashMap,LinkedHashMap,TreeMap都属于Map
Map 主要用于存储键(key)值(value)对,根据键得到值,因此键不允许键重复,但允许值重复。
HashMap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度。HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null;HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力。
LinkedHashMap LinkedHashMap也是一个HashMap,但是内部维持了一个双向链表,可以保持顺序
TreeMap 不仅可以保持顺序,而且可以用于排序
HashMap例子:
public static void main(String[] args) { //遍历:后放先出 Map<String, String> map = new HashMap<String, String>(); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
LinkedHashMap例子:
public static void main(String[] args) { //遍历:怎么样放怎么样出 Map<String, String> map = new LinkedHashMap<String, String>(); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
TreeMap例子:
public static void main(String[] args) { //遍历:不管怎么样放,出的时候都是按key的排序(升序)出。 Map<String, String> map = new TreeMap<String, String>(new Comparator<Object>(){ Collator collator = Collator.getInstance(); public int compare(Object o1, Object o2) { CollationKey key1 = collator.getCollationKey(o1.toString()); CollationKey key2 = collator.getCollationKey(o2.toString()); return key1.compareTo(key2); //return collator.compare(o1, o2); }}); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
- map的遍历,HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap LinkedHashMap TreeMap 的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别
- Windows下Github使用方法
- asp.net session的应用
- 数组指针和指针数组的区别
- 全库级 stream复制:maintain_global方法
- Git工具连接GitHub(Windows配置篇)
- map的遍历,HashMap,LinkedHashMap,TreeMap的区别
- stl仿函数
- 【C++】endl的作用
- hdu 1233 还是畅通工程
- JAVA多态主板示例
- 五种常见的 PHP 设计模式
- IE7不支持json 解决办法
- 2013年CSDN高校俱乐部全国巡讲
- 这里做个纪念吧