为什么两个看似相同的哈希映射在被gson序列化时具有不同的行为?
输入:
public static void main(String[] args) {
final String key = "some key";
final String value = "some value";
Map<String, String> map1 = new HashMap<String, String>(){{put(key, value);}};
System.out.println(new Gson().toJson(map1) + " " + map1.get(key));
Map<String, String> map2 = new HashMap<>();
map2.put(key, value);
System.out.println(new Gson().toJson(map2) + " " + map2.get(key));
}
输出:
null some value
{"some key":"some value"} some value
Process finished with exit code 0