HashMap,LinkedHashMap,TreeMap对比
来源:互联网 发布:淘宝二级页面是什么 编辑:程序博客网 时间:2024/05/29 16:21
相同点:
HashMap,LinkedHashMap,TreeMap都属于Map;用于存储键值对(key-value),根据键得到值。键不允许重复,值允许重复。
不同点:
- HashMap根据键的HashCode为索引存储数据。键值对取出时是随机的。
- TreeMap键值对取出是排序的。
- LinkedHashMap是HashMap的子类。键值对输入顺序和取出顺序是相同的。
测试代码:
public class MapTest {public static void main(String[] args) {try {hashMapTest();treeMapTest();likedHashMapTest();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void hashMapTest() throws Exception {System.out.println("--------HashMap--------");Map<String, String> map = new HashMap<String, String>();map.put("1", "map test 1");map.put("2", "map test 2");map.put("3", "map test 3");map.put("a", "map test a");map.put("b", "map test b");map.put("c", "map test c");Iterator<Entry<String, String>> it = map.entrySet().iterator();while (it.hasNext()) {Entry<String, String> entry = it.next();System.out.println("Key: " + entry.getKey() + " Value: "+ entry.getValue());}}public static void treeMapTest() throws Exception {System.out.println("--------TreeMap--------");Map<String, String> map = new TreeMap<String, String>();map.put("1", "map test 1");map.put("2", "map test 2");map.put("3", "map test 3");map.put("a", "map test a");map.put("b", "map test b");map.put("c", "map test c");Iterator<Entry<String, String>> it = map.entrySet().iterator();while (it.hasNext()) {Entry<String, String> entry = it.next();System.out.println("Key: " + entry.getKey() + " Value: "+ entry.getValue());}}public static void likedHashMapTest() throws Exception {System.out.println("--------LikedHashMap--------");Map<String, String> map = new LinkedHashMap<String, String>();map.put("1", "map test 1");map.put("2", "map test 2");map.put("3", "map test 3");map.put("a", "map test a");map.put("b", "map test b");map.put("c", "map test c");Iterator<Entry<String, String>> it = map.entrySet().iterator();while (it.hasNext()) {Entry<String, String> entry = it.next();System.out.println("Key: " + entry.getKey() + " Value: "+ entry.getValue());}}}
结果:
--------HashMap--------
Key: 1 Value: map test 1
Key: a Value: map test a
Key: 2 Value: map test 2
Key: b Value: map test b
Key: 3 Value: map test 3
Key: c Value: map test c
--------TreeMap--------
Key: 1 Value: map test 1
Key: 2 Value: map test 2
Key: 3 Value: map test 3
Key: a Value: map test a
Key: b Value: map test b
Key: c Value: map test c
--------LikedHashMap--------
Key: 1 Value: map test 1
Key: 2 Value: map test 2
Key: 3 Value: map test 3
Key: a Value: map test a
Key: b Value: map test b
Key: c Value: map test c
Key: 1 Value: map test 1
Key: a Value: map test a
Key: 2 Value: map test 2
Key: b Value: map test b
Key: 3 Value: map test 3
Key: c Value: map test c
--------TreeMap--------
Key: 1 Value: map test 1
Key: 2 Value: map test 2
Key: 3 Value: map test 3
Key: a Value: map test a
Key: b Value: map test b
Key: c Value: map test c
--------LikedHashMap--------
Key: 1 Value: map test 1
Key: 2 Value: map test 2
Key: 3 Value: map test 3
Key: a Value: map test a
Key: b Value: map test b
Key: c Value: map test c
阅读全文
0 0
- HashMap,LinkedHashMap,TreeMap对比
- HashMap,LinkedHashMap,TreeMap对比
- HashMap,LinkedHashMap,TreeMap对比
- HashMap、HashSet、Hashtable、concurrentHashmap、treemap、linkedhashmap对比
- Java中HashMap LinkedHashMap TreeMap的对比
- java学习笔记4 - HashMap,LinkedHashMap,TreeMap对比
- JDK容器学习之Map : HashMap,TreeMap,LinkedHashMap对比
- HashMap,LinkedHashMap,TreeMap简介
- HashMap,LinkedHashMap,TreeMap应用
- HashMap ,treeMap,LinkedHashMap区别
- treemap hashmap linkedhashmap
- Maps--HashMap, LinkedHashMap, TreeMap
- HashMap Hashtable LinkedHashMap TreeMap
- HashMap 、TreeMap、Hashtable、LinkedHashMap
- java HashMap TreeMap LinkedHashMap
- HashMap HashTable LinkedHashMap TreeMap
- HashMap,HashTable,LinkedHashMap,TreeMap
- HashMap,LinkedHashMap,TreeMap
- UICC之UiccController
- jsp基础
- Java&Android零碎的知识点2
- Btree索引详解
- Java应用Tomcat执行过程之性能调优
- HashMap,LinkedHashMap,TreeMap对比
- tiny-dnn源码Edge类
- Python图表绘制:matplotlib绘图库入门
- codeforces 454A Little Pony and Crystal Mine
- SpringBoot整合MyBatis
- Opengl RC(Render context,渲染上下文)与像素格式(转)
- Class字节码指令解释执行
- mysql常见面试题汇总
- 进程间通信(三)信号