简体   繁体   English

将Java中不同类型的HashMap合并为通用HashMap

[英]Combining different Types of HashMap in Java to a generic HashMap

My problems starts here with a limitation with HashMap in Java that does not allow using multiple values for same key. 我的问题始于Java中的HashMap限制,该限制不允许对同一键使用多个值。 As in my code written below i need to have multiple values for k5 & k6 . 如下面的代码所示,我需要k5 & k6具有多个值。

map is the main hashmap of QueryParameters that i will be passing on to be used in my RestAPI request. map是QueryParameters的主要哈希 ,我将继续将其用于RestAPI请求中。 Approach tried: Created different Map of String and String[] type and then merge which i found would not be allowed to be merged. 尝试的方法:创建String和String []类型的不同映射,然后合并,我发现将不允许合并。 Code of the same has been marked as commented via // 相同的代码已通过//标记为已注释

My REST request looks like the following and i am finding it difficult how to send it in the following manner: 我的REST请求如下所示,我发现如何以以下方式发送它很困难:

https://myURL/api&k1[]=value&k2=2value&k5[]=v3&k5[]=v4&k5[]=v5&k6[]=v7&k6[]=v8&k6[]=v8

If there is any other way to represent this in Java, please do let me know. 如果有其他方法可以用Java表示,请告诉我。

Map<String, String> map = new HashMap<>();
map.put("k1", "value");
map.put("k2", "2value");
//String[] k5 = new String[]{"v3", "v4", "v5"};
map.put("k5[]", "v3");
map.put("k5[]", "v4");
map.put("k5[]", "v5");
//String[] k6= new String[]{"v7", "v8", "v9"};
map.put("k6[]", "v7");
map.put("k6[]", "v8");
map.put("k6[]", "v9");

It pretty simple actually. 其实很简单。 Here's what you'll need to do: 这是您需要做的:

Map<String, String[]> map = new HashMap<>();
map.put("v3", new String[] { "v", "foo", "bar" });   

If you want to add more elements to this, you can define the map as Map<String, List<String>> 如果要为此添加更多元素,可以将地图定义为Map<String, List<String>>

Map<String, List<String>> map = new HashMap<>();
List<String> initialList = new ArrayList<>();
initialList.add("v");
initialList.add("foo");
initialList.add("bar");
map.put("v3", initialList);

// add some new String      
map.get("v3").add("Some_new_string");

// adding a whole new list
map.get("v3").addAll(Arrays.asList("new_element_1", "new_element_2"));

You want this map: 您想要这张地图:

Map<String, List<String>> strings = new HashMap<>();

For adding and removing strings, use computeIfAbsent and computeIfPresent : 要添加和删除字符串,请使用computeIfAbsentcomputeIfPresent

// Add str to list of strings indexed by key
strings.computeIfAbsent(key, ign -> new ArrayList<>()).add(str);

// Remove str from list of strings indexed by key
strings.computeIfPresent(key, (i, c) -> c.remove(str) && c.isEmpty() ? null : c);

This dynamically creates and removes the Lists in the map. 这将动态创建并删除地图中的列表。 Example: 例:

Map<String, List<String>> strings = new HashMap<>();
put(strings, "key", "a"); // ["key" => ["a"]] <-- list dynamically created
put(strings, "key", "b"); // ["key" => ["a", "b"]]
remove(strings, "key", "b"); // ["key" => ["a"]]
remove(strings, "key", "a"); // [] <-- no leftovers

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

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