Difference between HashMap, LinkedHashMap and TreeMap
来源:互联网 发布:网络舆情监测平台 编辑:程序博客网 时间:2024/05/16 08:55
All three classes implement the Map
interface and offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen:
HashMap
makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.TreeMap
will iterate according to the "natural ordering" of the keys according to theircompareTo()
method (or an externally suppliedComparator
). Additionally, it implements theSortedMap
interface, which contains methods that depend on this sort order.LinkedHashMap
will iterate in the order in which the entries were put into the map
"Hashtable" is the generic name for hash-based maps. In the context of the Java API, Hashtable
is an obsolete class from the days of Java 1.1 before the collections framework existed. It should not be used anymore, because its API is cluttered with obsolete methods that duplicate functionality, and its methods are synchronized (which can decrease performance and is generally useless). Use ConcurrrentHashMap instead of Hashtable.
╔══════════════╦═════════════════════╦═══════════════════╦══════════════════════╗║ Property ║ HashMap ║ TreeMap ║ LinkedHashMap ║╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣║ ║ no guarantee order ║ sorted according ║ ║║ Order ║ will remain constant║ to the natural ║ insertion-order ║║ ║ over time ║ ordering ║ ║╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣║ Get/put ║ ║ ║ ║║ remove ║ O(1) ║ O(log(n)) ║ O(1) ║║ containsKey ║ ║ ║ ║╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣║ ║ ║ NavigableMap ║ ║║ Interfaces ║ Map ║ Map ║ Map ║║ ║ ║ SortedMap ║ ║╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣║ ║ ║ ║ ║║ Null ║ allowed ║ only values ║ allowed ║║ values/keys ║ ║ ║ ║╠══════════════╬═════════════════════╩═══════════════════╩══════════════════════╣║ ║ Fail-fast behavior of an iterator cannot be guaranteed ║║ Fail-fast ║ impossible to make any hard guarantees in the presence of ║║ behavior ║ unsynchronized concurrent modification ║╠══════════════╬═════════════════════╦═══════════════════╦══════════════════════╣║ ║ ║ ║ ║║Implementation║ buckets ║ Red-Black Tree ║ double-linked ║║ ║ ║ ║ buckets ║╠══════════════╬═════════════════════╩═══════════════════╩══════════════════════╣║ Is ║ ║║ synchronized ║ implementation is not synchronized ║╚══════════════╩════════════════════════════════════════════════════════════════╝
package com.hashmap;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;public class HashmapTest{ public static void main(String[] args) { Map<String,String> map=new HashMap<>(); map.put("a", "aa"); Iterator<Entry<String,String>> iterator=map.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, String> entry = iterator.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } Map<String,String> treeMap=new TreeMap<>(); treeMap.put("b", "bb"); Iterator<Entry<String,String>> iterator2=treeMap.entrySet().iterator(); while (iterator2.hasNext()) { Entry<String,String> entry = iterator2.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } Map<String,String> linkedHashMap=new LinkedHashMap<>(); linkedHashMap.put("c", "cc"); Iterator<Entry<String,String>> iterator3=linkedHashMap.entrySet().iterator(); while (iterator3.hasNext()) { Entry<String,String> entry = iterator3.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } }}
0 0
- Difference between HashMap, LinkedHashMap and TreeMap
- Difference between HashMap, LinkedHashMap and TreeMap in Java
- Difference between TreeMap and TreeSet in Java
- Difference between HashMap and Hashtable in Java
- Difference between HashSet and HashMap in Java
- 6 Difference Between HashMap And HashTable
- HashMap,LinkedHashMap,TreeMap简介
- HashMap,LinkedHashMap,TreeMap应用
- HashMap ,treeMap,LinkedHashMap区别
- treemap hashmap linkedhashmap
- Maps--HashMap, LinkedHashMap, TreeMap
- HashMap Hashtable LinkedHashMap TreeMap
- HashMap 、TreeMap、Hashtable、LinkedHashMap
- HashMap,LinkedHashMap,TreeMap对比
- java HashMap TreeMap LinkedHashMap
- HashMap,LinkedHashMap,TreeMap对比
- HashMap HashTable LinkedHashMap TreeMap
- HashMap,HashTable,LinkedHashMap,TreeMap
- 魅族公布苏宁818战报:魅蓝3s斩获单品销量冠军
- Python中执行Hql脚本
- 仿京东商城侧边导航栏写法
- caffe学习(5)------caffe for ubuntu环境配置
- 网易有道2017内推编程题-Java实现
- Difference between HashMap, LinkedHashMap and TreeMap
- java8 四个核心函数式接口
- 11. Container With Most Water
- SVM技术详解(上)
- 数据结构之图---最小生成树Kruskal算法---C++实现
- MyEclipse 连接 数据库(Navicat)
- 深入剖析Java中的装箱和拆箱
- 装饰模式(python版)
- Python——常用方法