HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap 关于k,v是否为null,以及输出排序
来源:互联网 发布:社交网络第二定律 编辑:程序博客网 时间:2024/05/17 21:58
HashMap:k,v可为null,LinkedHashMap:k,v可为null,TreeMap:v可为null,HashTable:k,v都不可为null,ConcurrentHashMap:k,v都不可为null,ConcurrentSkipListMap:k,v都不可为null。
HashMap,HashTable,ConcurrentHashMap 为无序输出;TreeMap为key排序输出;LinkedHashMap put顺序先后输;ConcurrentSkipListMap为key排序输出。
下面代码测试:
public class MapKVIsNullTest { public static void main(String[] args) { // HashMap System.out.println("------HashMap无序输出------"); HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("3", null); hashMap.put("b", "Value1"); hashMap.put("2", "Value2"); hashMap.put("a", "ValueB"); hashMap.put("ee", "ValueA"); Iterator<Entry<String, String>> it = hashMap.entrySet().iterator(); while (it.hasNext()) { Entry<String, String> e = it.next(); System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue()); } // TreeMap System.out.println("------TreeMap按Key排序输出------"); TreeMap<String, String> teMap = new TreeMap<String, String>(); teMap.put("3", null); teMap.put("1", "Value1"); teMap.put("2", "Value2"); teMap.put("b", "ValueB"); teMap.put("a", "ValueA"); Iterator<Entry<String, String>> tit = teMap.entrySet().iterator(); while (tit.hasNext()) { Entry<String, String> e = tit.next(); System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue()); } // LinkedHashMap System.out.println("--LinkedHashMap根据输入的顺序输出--"); LinkedHashMap<String, String> lhsMap = new LinkedHashMap<String, String>(); lhsMap.put(null, null); // lhsMap.put("1", "Value1"); lhsMap.put("2", "Value2"); lhsMap.put("b", "ValueB"); lhsMap.put("a", "ValueA"); Iterator<Entry<String, String>> lit = lhsMap.entrySet().iterator(); while (lit.hasNext()) { Entry<String, String> e = lit.next(); System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue()); } Collections.emptyMap(); // HashTable System.out.println("--Hashtable无序输出--"); Hashtable<String, String> hstable = new Hashtable<String, String>(); hstable.put("3", "Value3"); hstable.put("1", "Value1"); hstable.put("2", "Value2"); hstable.put("b", "ValueB"); hstable.put("a", "ValueA"); Iterator<Entry<String, String>> ithstable = hstable.entrySet().iterator(); while (ithstable.hasNext()) { Entry<String, String> e = ithstable.next(); System.out.println("Key: " + e.getKey() + "--Value: " + e.getValue()); } // ConcurrentHashMap System.out.println("--ConcurrentHashMap无序输出--"); ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<String, String>(); concurrentHashMap.put("3", "Value3"); concurrentHashMap.put("b", "Value1"); concurrentHashMap.put("2", "Value2"); concurrentHashMap.put("a", "ValueB"); concurrentHashMap.put("ee", "ValueA"); for (Entry<String, String> v : concurrentHashMap.entrySet()) { System.out.println("Key: " + v.getKey() + "--Value: " + v.getValue()); } // ConcurrentSkipListMap System.out.println("--ConcurrentHashMap无序输出--"); ConcurrentSkipListMap<String, String> concurrentSkipListMap = new ConcurrentSkipListMap<String, String>(); concurrentSkipListMap.put("11", "Value3"); concurrentSkipListMap.put("b", "Value1"); concurrentSkipListMap.put("2", "Value2"); concurrentSkipListMap.put("a", "ValueB"); concurrentSkipListMap.put("ee", "ValueA"); for (Entry<String, String> v : concurrentSkipListMap.entrySet()) { System.out.println("Key: " + v.getKey() + "--Value: " + v.getValue()); } }}
0 0
- HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap 关于k,v是否为null,以及输出排序
- HashMap、HashSet、Hashtable、concurrentHashmap、treemap、linkedhashmap对比
- LinkedHashMap、HashMap、HashTable、TreeMap、ConcurrentHashMap的比较
- HashMap、LinkedHashMap、TreeMap、HashTable、ConcurrentHashMap原理解析
- HashMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap
- HashMap、HashTable 和 ConcurrentHashMap 的键值对<K,V>能否为null
- Java集合之Hashtable<K,V>,TreeMap<K,V>,HashMap<K,V>自动排序
- 关于TreeMap、HashMap、HashTable的键是否能为""和Null的问题
- 关于TreeMap、HashMap、HashTable的键是否能为\"\"和Null的问题
- HashMap Hashtable LinkedHashMap TreeMap
- HashMap 、TreeMap、Hashtable、LinkedHashMap
- HashMap HashTable LinkedHashMap TreeMap
- HashMap,HashTable,LinkedHashMap,TreeMap
- hashMap,hashTable,linkedHashMap,TreeMap
- 从源代码看TreeMap、HashMap、Hashtable、ConcurrentHashMap、LinkedHashMap特性
- HashMap、Hashtable、LinkedHashMap、TreeMap、ConcurrentHashMap之间的区别-yellowcong
- HashMap Hashtable LinkedHashMap 和TreeMap
- HashMap Hashtable LinkedHashMap 和TreeMap
- Negative value (-100) passed to setFirstResult
- sass和compass的安装
- Ubuntu 安装配置mysql-cluster
- Unity3D 封装自己的Debug类库dll
- PushbackInputStream
- HashMap,LinkedHashMap,TreeMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap 关于k,v是否为null,以及输出排序
- 2014年终总结回顾与2015年工作总结
- Linux命令之at CTRL+D使AT命令生效
- NPOI操作EXCEL----------NPOI基础01
- C++ 为什么设置基类的析构函数为虚函数
- Angularjs 源码分析-setupModuleLoader
- Android实现竖着的滑动刻度尺效果,选择身高(竖向的)Android实现滑动刻度尺效果,选择身高体重和生日------浅谈Andorid开发中的MVP模式
- java中什么是bridge method(桥接方法)
- Android Fragment 生命周期