Java Map初始化的方法
第一种 最常见的方式(新建Map对象)
public void demo(){
private Map<String, String> mMap = new HashMap<String, String>();
mMap.put("k1","v1");
mMap.put("k2","v2");
}
第二种 java8新特性-双括号初始化
public void demo(){
private Map<String, String> mMap = new HashMap<String, String>(){{
put("k1","v1");
put("k2","v2");
}}
}
第三种 java9新特性-最简便的方式
public void demo(){
Map.of("k1","v1","k2","v2")
}
第四种 Guava
public void demo(){
Map<Stirng, Stirng> mMap = ImmutableMap.of("k1","v1","k2","v2");
}