博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
阅读量:4683 次
发布时间:2019-06-09

本文共 2753 字,大约阅读时间需要 9 分钟。

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

关键技术剖析:
Map用于存储键值对,根据键得到值,因此不允许键重复,值可以重复。
l  (1)HashMap是一个最常用的Map,它根据键的hashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。HashMap最多只允许一条记录的键为null,不允许多条记录的值为null。HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。
l  (2)Hashtable与HashMap类似,不同的是:它不允许记录的键或者值为空;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,然而,这也导致了Hashtable在写入时会比较慢。
l  (3)LinkedHashMap保存了记录的插入顺序,在用Iteraor遍历LinkedHashMap时,先得到的记录肯定是先插入的。在遍历的时候会比HashMap慢。有HashMap的全部特性。
l  (4)TreeMap能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器。当用Iteraor遍历TreeMap时,得到的记录是排过序的。TreeMap的键和值都不能为空。

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

LinkedHashMap 是HashMap的一个子类,如果需要输出的顺序和输入的相同

public static void main(String[] args) {       Map
map = new HashMap
(); 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
> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry
e = it.next(); System.out.println("Key: " + e.getKey() + "; Value: " + e.getValue()); } System.out.println("======================================"); Map
map1 = new TreeMap
(); map1.put("1", "Level 1"); map1.put("2", "Level 2"); map1.put("3", "Level 3"); map1.put("a", "Level a"); map1.put("b", "Level b"); map1.put("c", "Level c"); Iterator
> it1 = map1.entrySet().iterator(); while (it1.hasNext()) { Map.Entry
e1 = it1.next(); System.out.println("Key: " + e1.getKey() + "; Value: " + e1.getValue()); } System.out.println("======================================"); Map
map2 = new LinkedHashMap<>(); map2.put("1", "Level 1"); map2.put("2", "Level 2"); map2.put("3", "Level 3"); map2.put("a", "Level a"); map2.put("b", "Level b"); map2.put("c", "Level c"); Iterator
> it2 = map1.entrySet().iterator(); while (it2.hasNext()) { Map.Entry
e2 = it2.next(); System.out.println("Key: " + e2.getKey() + "; Value: " + e2.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

转载于:https://www.cnblogs.com/cuixiaomeng/p/7747683.html

你可能感兴趣的文章
[题目] Luogu P3716 [CTSC2000]冰原探险
查看>>
linux下用phpize给PHP动态添加扩展
查看>>
php session 严格过期时间实现
查看>>
基于源码学习-fighting
查看>>
[转]LINUX新建和增加SWAP分区
查看>>
(上线时清缓存)laravel 5.1 的程序性能优化(配置文件) - 简书
查看>>
SettingsSVNPlugin
查看>>
华为经典问题汇总~
查看>>
linux桌面环境gnome,kde,xfce,lxde 使用比较(转)
查看>>
如何做自己不想做的事情,却必须要去做的事情
查看>>
JavaScript的深入理解(1)
查看>>
Go-TCP粘包
查看>>
KNN算法的感受 1
查看>>
用Maven构建Mahout项目实现协同过滤userCF--单机版
查看>>
Java多线程-线程的调度(守护线程)
查看>>
Bootstrap 简介(Web前端CSS框架)
查看>>
Bootstrap 概览
查看>>
nginx配置ssl证书实现https访问
查看>>
c# while穷举(凑钱)
查看>>
EnCase v7 could not recognize Chinese character folder names / file names on Linux Platform
查看>>