Map 的 key、value 是否允许为null
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Map 的 key、value 是否允许为null
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                Map的key和value是否允許null?
直接寫程序驗證一下:
import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap;/*** @author robin*/ public class MyTest {public static void main(String[] args) {Map<Object, Object> m1 = new HashMap<>();Map<Object, Object> m2 = new TreeMap<>();Map<Object, Object> m3 = new Hashtable<>();Map<Object, Object> m4 = new ConcurrentHashMap<>();try {m1.put("zzz", null);} catch (Exception e) {System.out.println("m1-a:" + e.getMessage());}try {m1.put(null, null);} catch (Exception e) {System.out.println("m1-b:" + e.getMessage());}try {m2.put("zzz", null);} catch (Exception e) {System.out.println("m2-a:" + e.getMessage());}try {m2.put(null, null);} catch (Exception e) {System.out.println("m2-b:" + e.getMessage());}try {m3.put("zzz", null);} catch (Exception e) {System.out.println("m3-a:" + e.getMessage());}try {m3.put(null, null);} catch (Exception e) {System.out.println("m3-b:" + e.getMessage());}try {m4.put("zzz", null);} catch (Exception e) {System.out.println("m4-a:" + e.getMessage());}try {m4.put(null, null);} catch (Exception e) {System.out.println("m4-b:" + e.getMessage());}}}執(zhí)行結果:
?
m2-b:null m3-a:null m3-b:null m4-a:null m4-b:null結論如下表:
轉載于:https://www.cnblogs.com/amunote/p/10211760.html
總結
以上是生活随笔為你收集整理的Map 的 key、value 是否允许为null的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: apache 二级域名设置
- 下一篇: 标准化、归一化
