Java9实战Map.of注意事项解决Map.of注意事项1Map.of("a", null) // NPE解释:Java9的Map.of构造方法,参数的key和value都不能为null解决自己构造HashMap123456789101112@SuppressWarnings("unchecked")public static <K, V> Map<K, V> of(Object... kv) { if ((kv.length & 1) == 1) { throw new IllegalArgumentException(); } Map<K, V> map = new HashMap<>(); for (int i = 0; i < kv.length; i += 2) { map.put((K) kv[i], (V) kv[i + 1]); } return map;} 技术 / Java2024-12-30