简体   繁体   English

在Java中遍历hashmap时确定数据类型

[英]determine data type while iterating through hashmap in java

Is it possible to inspect data type of value while iterating through HashMap<String,Object> ? 通过HashMap<String,Object>迭代时是否可以检查value的数据类型?

while(iterator.hasNext) {
    Map.Entry pairs = (Map.Entry) iterator.next();
    String key = pairs.getKey();
   <Type> value = (Type) pairs.getValue();        
}

The map has a combination of Boolean , String and ArrayList<String> types as values. 该映射具有BooleanStringArrayList<String>类型的组合作为值。 I would like to determine the data type of the value at runtime and properly cast it. 我想在运行时确定值的数据类型并将其正确转换。

我建议您使用像Jackson或GSON这样的对象映射器在json和您的对象之间进行转换,而不要手动执行。

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

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