简体   繁体   English

如何从具有不同值类型的Map中获取值? (JAVA)

[英]How to get values from a Map with different value types? (Java)

I have a utility function that returns Map<String, Object> , but that Map always has this structure: 我有一个实用函数,它返回Map<String, Object> ,但Map始终具有以下结构:

{ "a": <something of type1>, "b": <something of type2> }

In outer function, where I call this utility function, I want to get the value associated with key a . 在外部函数中,我将其称为效用函数,我想获得与键a关联的值。 I'm trying to do something such as: 我正在尝试执行以下操作:

Map<String, Object> data = findData(input); // findData is the utility function
Type1 type1obj = data.get("a"); // error here

But then I get the Incompatible Types error: 但是,然后我得到不兼容类型错误:

Required: com.blah.blah.Type1
Found: java.lang.Object

How am I supposed to grab the value associated with key a ? 我应该如何获取与密钥a关联的值? FYI: I'm VERY new to Java. 仅供参考:我是Java的新手。

You need to cast the Object retrieved from Map to the type you required: 您需要将从Map检索到的对象转换为所需的类型:

Type1 type1obj = (Type1) data.get("a");

But with the code above you have to make sure the type of value associated with key "a" is Type1, otherwise a ClassCastException will thrown at runtime. 但是,使用上面的代码,您必须确保与键“ a”关联的值的类型为Type1,否则将在运行时引发ClassCastException

If you cannot guarantee the type of the Object retrieved, you can check it like: 如果您不能保证检索到的对象的类型,可以按照以下方式进行检查:

Object obj = data.get("a");
Type1 type1obj;
if(obj instanceof Type1) {
    type1obj = (Type1) obj;
} else {
    //to print error log or do some workaround
}

Ultimately, because your map has Object as its value type, you need to cast, but you can hide this with a typed utility method: 最终,由于地图将Object作为其值类型,因此需要进行转换,但是可以使用类型化的实用程序方法将其隐藏:

@SuppressWarnings("unchecked")
public static <T> T get(Map<String, Object> map, String key) {
    return (T)map.get(key);
}

So then due to java's type inference this will compile: 因此,由于java的类型推断,它将进行编译:

Type1 type1obj = get(data, "a");

Adding @SuppressWarnings("unchecked") stops your IDE/compiler complaining about an "unsafe cast". 添加@SuppressWarnings("unchecked")阻止您的IDE /编译器抱怨“不安全的转换”。

Basically whenever I reach this point of storing with a HashMap or any map for that sort. 基本上,每当我达到使用HashMap或此类的任何地图进行存储的时候。 I tend to go ahead and cast it. 我倾向于将其投射。 Take for example I have a class named Module that I am getting the value of and the key is the module name. 例如,我有一个名为Module的类,该类正在获取其值,键是模块名。 I would go (Module) map.get(modName); 我去(模块)map.get(modName);

暂无
暂无

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

相关问题 如何在Map中使用具有不同类型值的泛型 - How to use generics in a Map with values of different types 如何从Java中的Map键获取值 - How to get value from Map key in java 在Java中的映射中将不同类类型的对象作为值处理 - Handling objects of different class types as values in a map in java java - 如何从Java中的Map值列表中获取值并在它不存在时进行处理 - How to get the value from List of Map values in java and handle when it is not present 如何在Java中使用Excel从column1和column 3获取单元格值并将映射作为键值对放入地图 - How to get the cell values from column1 and column 3 and put in map as key value pair using excel in java 如何使用不同类型的元素从 arraylist 中获取特定值? - How to get a specific value from arraylist with different types of elements? 如何从java地图中获取价值:地图 <String,ArrayList<String> &gt;? - How to get value from the java map : Map<String,ArrayList<String>>? 在Spring Boot Java项目中,如何将不同数据类型的属性从Mongo DB映射到Java类? - In spring boot java project how to map a property of different data types from a mongo DB to java class? Java - 将不同类型的2个键映射到相同的值(任何类型)。 我该怎么办呢? - Java - map 2 keys of different types to the same value (any type). How can I go about it? 使用Java中的Jackson将具有不同类型值的映射序列化为JSON - Serializing map having value of different types to JSON using Jackson in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM