参考:https://baike.xsoftlab.net/view/250.html
package com.dh.learn.map; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; public class LearnHashMap { public static void main(String[] args) { //无序 //key/value 均可以放null //put两个相同key时,后边的value会覆盖前面的 //非线程安全 Map<String, String> map = new HashMap<>(); map.put(null, "123"); map.put("345", "345"); map.put("123", null); map.put("234", "345"); map.put("234", "234"); System.out.println(map); // {null=123, 123=null, 234=234, 345=345} Map<String, String> table = new Hashtable<>(); //不能放null元素 //线程安全 // 抛异常,key不能为null // table.put(null, "123"); table.put("345", "345"); // 抛异常,value不能为null // table.put("123", null); table.put("234", "345"); table.put("234", "234"); System.out.println(table); for (Map.Entry<String, String> entry : table.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } }