HashMap,LinkedHashMap,TreeMap对比
2017-09-17 15:59
375 查看
相同点:
HashMap,LinkedHashMap,TreeMap都属于Map;用于存储键值对(key-value),根据键得到值。键不允许重复,值允许重复。
不同点:
HashMap根据键的HashCode为索引存储数据。键值对取出时是随机的。
TreeMap键值对取出是排序的。
LinkedHashMap是HashMap的子类。键值对输入顺序和取出顺序是相同的。
测试代码:
结果:
--------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
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 block e.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
相关文章推荐
- HashMap、LinkedHashMap和TreeMap对比
- HashMap,LinkedHashMap,TreeMap对比
- HashMap,LinkedHashMap,TreeMap对比
- java学习笔记4 - HashMap,LinkedHashMap,TreeMap对比
- HashMap,LinkedHashMap,TreeMap对比
- HashMap 、TreeMap、Hashtable、LinkedHashMap
- HashMap、HashTable、LinkedHashMap和TreeMap的区别
- JAVA中HashMap和LinkedHashMap以及TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的区别+如何替换LinkedHashMap键位的值+RecyclerView单条更新
- Java中HashMap,LinkedHashMap,TreeMap的区别
- Java中hashmap、linkedhashmap、treemap的区别
- HashMap,LinkedHashMap,TreeMap,HashTable的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- HashMap,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap的有序性
- LinkedHashMap、HashMap、HashTable、TreeMap、ConcurrentHashMap的比较
- HashMap,LinkedHashMap,TreeMap,HashTable的区别
- JDK源码学习(4)-java.util.HashMap、LinkedHashMap与TreeMap