如何组合包含不同类型的两个HashMap对象?

时间:2021-06-18 12:20:25

I have two HashMap objects defined as:

我有两个HashMap对象定义为:

Map<String, String> requestParams = new HashMap<>();
Map<String, Boolean> requestParamForOauth = new HashMap<>();

How can I merge these two maps?

如何合并这两张地图?

4 个解决方案

#1


0  

Your keys are of the same type (String), but the values are not even related by an interface or super class, you will need to define a Map<String, Object> and make use of the Map#putAll method

您的键具有相同的类型(String),但是这些值甚至不与接口或超类相关,您需要定义Map 并使用Map#putAll方法 ,object>

Map<String, String> requestParams = new HashMap<>();
Map<String, Boolean> requestParamForOauth = new HashMap<>();
Map<String, Object> requestParamForOauth2 = new HashMap<>();
requestParamForOauth2.putAll(requestParams);
requestParamForOauth2.putAll(requestParamForOauth);

#2


1  

Assuming that both maps contain the same set of keys, and that you want to "combine" the values, the thing you would be looking for is a Pair class, see here for example.

假设两个映射都包含相同的键集,并且您想要“组合”这些值,那么您要查找的是Pair类,请参见此处。

You simply iterate one of the maps; and retrieve values from both maps; and create a Pair; and push that in your result map.

你只需迭代其中一张地图;并从两个地图中检索值;并创建一对;并在结果图中推送它。

The only downside is that there is no "official" Pair class that you could use (see here for more thoughts around that).

唯一的缺点是没有你可以使用的“官方”Pair类(请参阅此处以了解更多相关内容)。

Alternatively, if there is a "deeper" meaning of those "combined" values (beyond a simple "tuple/pair" semantics), you could instead create your own class that wraps around those two values.

或者,如果这些“组合”值存在“更深层”的含义(超出简单的“元组/对”语义),您可以创建自己的包含这两个值的类。

#3


0  

If you want to use one list to store all the data You can use one HashMap<String,Object>

如果要使用一个列表来存储所有数据,可以使用一个HashMap ,object>

#4


0  

What do you want to do when the same key appears in both Map? If you want to keep both the String and Boolean, then you'll need a map that looks like this: Map<String, Pair(String, Boolean)>. If you just want to keep one value, then Map<String, Object> is what you want.

当两个地图中出现相同的密钥时,您想要做什么?如果你想保留String和Boolean,那么你需要一个如下所示的地图:Map 。如果您只想保留一个值,那么Map 就是您想要的。 ,object> ,pair(string,boolean)>

#1


0  

Your keys are of the same type (String), but the values are not even related by an interface or super class, you will need to define a Map<String, Object> and make use of the Map#putAll method

您的键具有相同的类型(String),但是这些值甚至不与接口或超类相关,您需要定义Map 并使用Map#putAll方法 ,object>

Map<String, String> requestParams = new HashMap<>();
Map<String, Boolean> requestParamForOauth = new HashMap<>();
Map<String, Object> requestParamForOauth2 = new HashMap<>();
requestParamForOauth2.putAll(requestParams);
requestParamForOauth2.putAll(requestParamForOauth);

#2


1  

Assuming that both maps contain the same set of keys, and that you want to "combine" the values, the thing you would be looking for is a Pair class, see here for example.

假设两个映射都包含相同的键集,并且您想要“组合”这些值,那么您要查找的是Pair类,请参见此处。

You simply iterate one of the maps; and retrieve values from both maps; and create a Pair; and push that in your result map.

你只需迭代其中一张地图;并从两个地图中检索值;并创建一对;并在结果图中推送它。

The only downside is that there is no "official" Pair class that you could use (see here for more thoughts around that).

唯一的缺点是没有你可以使用的“官方”Pair类(请参阅此处以了解更多相关内容)。

Alternatively, if there is a "deeper" meaning of those "combined" values (beyond a simple "tuple/pair" semantics), you could instead create your own class that wraps around those two values.

或者,如果这些“组合”值存在“更深层”的含义(超出简单的“元组/对”语义),您可以创建自己的包含这两个值的类。

#3


0  

If you want to use one list to store all the data You can use one HashMap<String,Object>

如果要使用一个列表来存储所有数据,可以使用一个HashMap ,object>

#4


0  

What do you want to do when the same key appears in both Map? If you want to keep both the String and Boolean, then you'll need a map that looks like this: Map<String, Pair(String, Boolean)>. If you just want to keep one value, then Map<String, Object> is what you want.

当两个地图中出现相同的密钥时,您想要做什么?如果你想保留String和Boolean,那么你需要一个如下所示的地图:Map 。如果您只想保留一个值,那么Map 就是您想要的。 ,object> ,pair(string,boolean)>