共同点:

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

不同点:

1.HashMap里面存入的键值对在取出的时候是随机的,也是我们最常用的一个Map.它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。在Map 中插入、删除和定位元素,HashMap 是最好的选择。

2.TreeMap取出来的是排序后的键值对。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。

3. LinkedHashMap 是HashMap的一个子类,如果需要输出的顺序和输入的相同,那么用LinkedHashMap可以实现.  (应用场景:购物车等需要顺序的)

代码实例:

package com.alibaba.sample.petstore.web.store.module.screen;  

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; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; public class ViewCart {
@Autowired
private HttpServletResponse response; public void execute() throws Exception {
this.useHashMap();
this.useTreeMap();
this.useLikedHashMap();
} public void useHashMap() throws Exception {
response.getWriter().println("------无序(随机输出)------");
Map<String, String> map = new HashMap<String, String>();
map.put("1", "Level 1");
map.put("2", "Level 2");
map.put("3", "Level 3");
map.put("a", "Level a");
map.put("b", "Level b");
map.put("c", "Level c");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> e = it.next();
response.getWriter().println("Key: " + e.getKey() + "; Value: " + e.getValue());
}
} // 有序(默认排序,不能指定)
public void useTreeMap() throws Exception {
response.getWriter().println("------有序(但是按默认顺充,不能指定)------");
Map<String, String> map = new TreeMap<String, String>();
map.put("1", "Level 1");
map.put("2", "Level 2");
map.put("3", "Level 3");
map.put("a", "Level a");
map.put("b", "Level b");
map.put("c", "Level c");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> e = it.next();
response.getWriter().println("Key: " + e.getKey() + "; Value: " + e.getValue());
}
} public void useLikedHashMap() throws Exception {
response.getWriter().println("------有序(根据输入的顺序输出)------");
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("1", "Level 1");
map.put("2", "Level 2");
map.put("3", "Level 3");
map.put("a", "Level a");
map.put("b", "Level b");
map.put("c", "Level c");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> e = it.next();
response.getWriter().println("Key: " + e.getKey() + "; Value: " + e.getValue());
}
}
}

返回结果:

------无序(随机输出)------
Key: 3; Value: Level 3
Key: 2; Value: Level 2
Key: 1; Value: Level 1
Key: b; Value: Level b
Key: c; Value: Level c
Key: a; Value: Level a
------有序(但是按默认顺充,不能指定)------
Key: 1; Value: Level 1
Key: 2; Value: Level 2
Key: 3; Value: Level 3
Key: a; Value: Level a
Key: b; Value: Level b
Key: c; Value: Level c
------有序(根据输入的顺序输出)------
Key: 1; Value: Level 1
Key: 2; Value: Level 2
Key: 3; Value: Level 3
Key: a; Value: Level a
Key: b; Value: Level b
Key: c; Value: Level c

HashMap、LinkedHashMap和TreeMap对比的更多相关文章

  1. HashMap,LinkedHashMap,TreeMap对比

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

  2. Android——ArrayList 、LinkList、List 区别 & 迭代器iterator的使用 & HashMap、Hashtable、LinkedHashMap、TreeMap

     ArrayList .LinkList.List 区别 & 迭代器iterator的使用 & HashMap.Hashtable.LinkedHashMap.TreeMap 一.几个 ...

  3. HashMap、Hashtable、LinkedHashMap、TreeMap、ConcurrentHashMap的区别

    Map是Java最常用的集合类之一.它有很多实现类,我总结了几种常用的Map实现类,如下图所示.本篇文章重点总结几个Map实现类的特点和区别: 特点总结: 实现类 HashMap LinkedHash ...

  4. HashMap、HashTable、LinkedHashMap和TreeMap用法和区别

    Java为数据结构中的映射定义了一个接口java.util.Map,它有四个实现类,分别是HashMap.HashTable.LinkedHashMap和TreeMap.本节实例主要介绍这4中实例的用 ...

  5. Java中HashMap,LinkedHashMap,TreeMap的区别[转]

    原文:http://blog.csdn.net/xiyuan1999/article/details/6198394 java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类 ...

  6. HashMap,LinkedHashMap,TreeMap的区别(转)

    Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快 ...

  7. map,set,list等集合解析以及HashMap,LinkedHashMap,TreeMap等该选谁的的区别

    前言: 今天在整理一些资料时,想起了map,set,list等集合,于是就做些笔记,提供给大家学习参考以及自己日后回顾. Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允 ...

  8. Java HashMap,LinkedHashMap,TreeMap

    Java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMapMap主要用于存储健值对,根据 ...

  9. HashMap,LinkedHashMap,TreeMap之间的区别

    java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMap . Map 主要用于存储键( ...

随机推荐

  1. hdu5728

    详细题解: http://blog.csdn.net/wust_zzwh/article/details/51966450 ……化简公式的能力还不够啊…… #include<bits/stdc+ ...

  2. sublime text按esc经常进入command mode(不能输入任何东西)

    在使用sublime text进行 选中 操作中,如果使用了esc退出选中状态,会进入command mode,现象是不能输入任何东西,关闭当前编辑文件重新打开可以解决.但是很影响连贯性.可以通过一些 ...

  3. 【剑指offer】面试题 57. 和为 S 的数字

    面试题 57. 和为 S 的两个数字 题目一:和为 S 的两个数字 题目 输入一个递增排序的数组和一个数字 S,在数组中查找两个数,是的他们的和正好是 S,如果有多对数字的和等于 S, 输出两个数的乘 ...

  4. The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack

    题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...

  5. scrapy抓取拉勾网职位信息(一)——scrapy初识及lagou爬虫项目建立

    本次以scrapy抓取拉勾网职位信息作为scrapy学习的一个实战演练 python版本:3.7.1 框架:scrapy(pip直接安装可能会报错,如果是vc++环境不满足,建议直接安装一个visua ...

  6. Android Theme.AppCompat.Light的解决方法

    styles.xml中<style name="AppBaseTheme" parent="Theme.AppCompat.Light">提示如下错 ...

  7. Single Number II(LintCode)

    Single Number II Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Examp ...

  8. 洛谷——P1208 [USACO1.3]混合牛奶 Mixing Milk

    P1208 [USACO1.3]混合牛奶 Mixing Milk 题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业 ...

  9. Why Did the Cow Cross the Road III(树状数组)

    Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The lay ...

  10. shell 读配置文件

    今天跟同事探讨了一下 shell 脚本中对配置文件的读写问题.在此总结一下常用的配置文件的读写方式.大多数的配置文件都是以key=value形式存在的.配置项完全由键值对组成.这样的配置文件读写也是最 ...