HashMap, TreeMap, LinkedHashMap, Hashtable区别(一)
2013-10-29 00:00
387 查看
虽然都继承自Map接口,但是差别还是蛮大的。
对于容器类的研究,我想Thinking in Java的容器分类图说明的再明白不过。
接下来通过例子说明一下,具体的不同。
首先是对于null的处理。如下所示,K = Key, V = Value.注意Hashtable not HashTable
Java代码
对于容器类的研究,我想Thinking in Java的容器分类图说明的再明白不过。
接下来通过例子说明一下,具体的不同。
首先是对于null的处理。如下所示,K = Key, V = Value.注意Hashtable not HashTable
Class | K null V null | K null V null | K not null V null |
HashMap | OK | OK | OK |
Hashtable | NO | NO | NO |
TreeMap | NO | OK | OK |
LinkedHashMap | OK | NO | NO |
import java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.TreeMap; public class MapCompare { public static void main(String args[]) { testNull(); } public static void testNull(){ testHashMap(); testHashTable(); testTreeMap(); testLinkedHashMap(); } public static void testHashMap() { HashMap map = new HashMap(); // map.put(null, null); //map.put(null, "TEST"); map.put("TEST", null); } public static void testHashTable() { try { Hashtable map = new Hashtable(); // map.put(null, null); // map.put(null, "TEST"); map.put("TEST", null); } catch (Exception e) { e.printStackTrace(); } } public static void testTreeMap() { try { TreeMap map = new TreeMap(); // map.put(null, null); // map.put(null, "TEST"); map.put("TEST", null); } catch (Exception e) { e.printStackTrace(); } } public static void testLinkedHashMap() { try { LinkedHashMap map = new LinkedHashMap(); // map.put(null, null); // map.put(null, "TEST"); map.put("TEST", null); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- java基础:HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- Java中Hashmap,Hashtable, LinkedHashmap, TreeMap区别
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- HashMap、TreeMap、LinkedHashMap、hashtable的区别
- HashMap、LinkedHashMap、TreeMap、HashTable的区别
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- HashMap、LinkedHashMap、Hashtable和TreeMap用法和区别
- HashMap、TreeMap、LinkedHashMap、hashtable的区别
- HashTable,HashMap,LinkedHashMap,TreeMap之间的区别。
- HashMap,LinkedHashMap,Hashtable,TreeMap等区别
- HashMap,LinkedHashMap,TreeMap,HashTable的区别
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- HashMap、TreeMap、LinkedHashMap、hashtable的区别
- HashMap, TreeMap, LinkedHashMap, Hashtable区别(二)
- HashMap、Hashtable、LinkedHashMap、TreeMap、ConcurrentHashMap之间的区别-yellowcong
- java中HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap的区别和内部实现机制
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别