简体   繁体   English

整数对象 <String, String> 哈希图

[英]Integer object in a <String, String> hashmap

  1. Create a (String, Object) hashMap and put values of different types. 创建一个(String,Object)hashMap并放置不同类型的值。
  2. Serialize the hashmap into a json string and convert back into a hashmap of (String, String) 将哈希图序列化为json字符串,然后转换回为(String,String)的哈希图

When I try to get the value of key "key2", it will throw an exception saying "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String". 当我尝试获取键“ key2”的值时,它将引发一个异常,指出“线程“ main”中的异常java.lang.ClassCastException:java.lang.Integer无法转换为java.lang.String”。

Is there a good way to explain this? 有什么好办法解释吗? Does it mean even though I have a (String, String) map, the value in it is not necessary String? 这是否意味着即使我有一个(String,String)映射,其中的值也不一定是String?

Apologize for any confusion. 如有任何混淆,敬请原谅。 Let me know if something is not clear. 让我知道是否不清楚。

Map<String, Object> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", 99);
map.put("key3", new Date());


JsonUtil jsonUtil = new JsonUtil();

String s = jsonUtil.toJson(map);

HashMap<String, String> newMap = jsonUtil.fromJson(s, HashMap.class);

String value = newMap.get("key2");

In map, the value of key "key2" is 99, which is an integer. 在map中,键“ key2”的值为99,它是一个整数。 The exception is possibly being thrown in the second to last line, not the last line. 可能在倒数第二行而不是最后一行中引发异常。 I'm not entirely sure why you are serializing and then immediately deserializing, but if you just want 99 as a String like "99", the way to do this would be: 我不完全确定为什么要先序列化然后立即反序列化,但是如果您只想将99作为“ 99”之类的字符串,执行此操作的方法是:

String value = String.valueOf(map.get("key2"));

The serializing and deserializing would not be necessary in this case. 在这种情况下,不需要序列化和反序列化。

Better yet, if you just want a Map<String, String> for the whole time, you can do something like this: 更好的是,如果您一直想要Map<String, String> ,则可以执行以下操作:

Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", String.valueOf(99));
map.put(new Date().toString());

When you call fromJson(s, HashMap.class) , you're not providing any generic type information to the decoder--all it sees is HashMap , and the implicit return type from that call is HashMap<?,?> . 当您从fromJson(s, HashMap.class)调用时,您没有向解码器提供任何泛型类型信息-它看到的只是HashMap ,而该调用的隐式返回类型是HashMap<?,?> JSON can assume string keys, and the decoder is doing its best to decode objects into their "native" representations, so when you tell the compiler that all of the keys and values are strings, there's a mismatch. JSON可以假定字符串键,并且解码器正在尽最大努力将对象解码为它们的“本机”表示形式,因此当您告诉编译器所有键和值都是字符串时,就会出现不匹配的情况。

If you specify which JsonUtil is involved, we may be able to provide a more specific approach; 如果您指定涉及哪个 JsonUtil ,我们也许可以提供一种更具体的方法。 otherwise, you could use something like a stream transform to convert every value to v.toString() . 否则,您可以使用类似流转换的方法将每个值转换为v.toString()

String value = newMap.get("key2");

In this line you are getting the value of key2 in hashMap and then assigning it to a String type variable value which is invalid. 在这一行中,您将在hashMap中获取key2的值,然后将其分配给无效的String类型变量value Because the value of key2 is Integer type, you cannot do this directly. 因为key2的值是Integer类型,所以您不能直接执行此操作。 You can put this value in `Integer type variable. 您可以将此值放入`Integer type variable。 as bellow: 如下:

int value = newMap.get("key2");

Or if you want a generic solution so that you can assign any type of variable into value then you can declare it as Object type. 或者,如果您想要一个通用的解决方案,以便可以将任何类型的变量分配给value则可以将其声明为Object类型。 as bellow: 如下:

Object value = newMap.get("key2");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM