共同点:

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. [].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16) })

    问问你自己,看得懂这行代码吗?要是看不懂就点击进来看看吧,要是看的懂得话,可以绕路 1.call:call(thisObj,arg1,arg2,arg3) [].forEach.call($$(&qu ...

  2. Python 分页功能

    自定义分页组件 """ 自定义分页组件的使用方法: pager_obj = Pagination(request.GET.get('page',1),len(HOST_L ...

  3. 机器学习方法(七):Kmeans聚类K值如何选,以及数据重抽样方法Bootstrapping

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入.我的博客写一些自己用得到东西,并分享给 ...

  4. asp.net获取相对文件的绝对路径

    HttpContext.Current.Server.MapPath("/sign/");

  5. PHP策略设计模式

    <?php /** 抽象策略角色,以接口实现 */ interface Strategy { /** 算法接口 */ public function algorithmInterface(); ...

  6. 戴尔笔记本Inspiron 7560(灵越) 加装固态硬盘从选购固态硬盘到系统迁移到设置SSD为第一启动(受不了了,网上的教程就没有完整的)

    菜鸡我的笔记本为戴尔灵越Inpsiron 7560,其实Inspiron 15 7560 和Inspiron 7560是同一个型号. 电脑拆了安过内存条,换过电池,现在又加了一块固态硬盘. 因为不想安 ...

  7. 洛谷 P2680 运输计划-二分+树上差分(边权覆盖)

    P2680 运输计划 题目背景 公元 20442044 年,人类进入了宇宙纪元. 题目描述 公元20442044 年,人类进入了宇宙纪元. L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条 ...

  8. POJ3468 A Simple Problem with Interger [树状数组,差分]

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  9. upm配置文件

    组件配置说明 Ø 配置文件规范 Ø 组件分为公共组件和私有组件,分别在public段和private段,如下所示. <?xml version="1.0" encoding= ...

  10. 判断数独是否合法(LintCode)

    判断数独是否合法 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用. 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填 ...