[英]How to create a HashMap of HashMaps by switching one of the value as key
I have a map of maps as follows:- myMap = {cfds2={fname=ck1, uname=jk1, tenant=ABC1}, cfds1={fname=ck, uname=jk, tenant=ABC}}
and expecting a returned map as :- {tenant={fname=ck, handler=cfds1, uname=jk, tenant=ABC}}
when getIoiProperties(String tenant) is being called with ABC .我有一张地图如下:- myMap = {cfds2={fname=ck1, uname=jk1, tenant=ABC1}, cfds1={fname=ck, uname=jk, tenant=ABC}}
并期待返回的地图as :- {tenant={fname=ck, handler=cfds1, uname=jk, tenant=ABC}}
当 getIoiProperties(String tenant) 被ABC调用时。 So Create a new map by using one of the key of the inner map to be key of a newly created map and key of the outer map becomes one of the values.因此,通过使用内部映射的键之一作为新创建的映射的键和外部映射的键成为值之一来创建新映射。
import com.google.common.collect.Maps;
import java.util.Map;
public class MapTest
{
public Map<String, Map<String, String>> getIoiProperties(String tenant)
{
Map<String, String> tenantMap = Maps.newHashMap();
tenantMap.put("tenant", "ABC");
tenantMap.put("uname", "jk");
tenantMap.put("fname", "ck");
//Map of Maps
Map<String, Map<String, String>> myMap = Maps.newHashMap();
myMap.put("cfds1", tenantMap);
Map<String, String> tenantMap1 = Maps.newHashMap();
tenantMap1.put("tenant", "ABC1");
tenantMap1.put("uname", "jk1");
tenantMap1.put("fname", "ck1");
myMap.put("cfds2", tenantMap1);
System.out.println("myMap = " + myMap);
Map<String, Map<String, String>> result = Maps.newHashMap();
//iterate map
for (Map.Entry<String, Map<String, String>> entry : myMap.entrySet()) {
String key = entry.getKey();
for (Map.Entry<String, String> entry1 : entry.getValue().entrySet())
if (entry1.getKey().equals("tenant") && entry1.getValue().equals(tenant)) {
entry.getValue().put("handler", key);
result.put(entry1.getKey(), entry.getValue());
}
}
System.out.println(result);
System.out.println("myMap = " + myMap);
return result;
}
public static void main(String[] args) {
MapTest mp = new MapTest();
Map<String, Map<String, String>> res = mp.getIoiProperties("ABC");
System.out.println(res.get("tenant"));
}
} }
Before reading my algorithm, note that I did not check if the conditionals in the for loops work properly, that is on you.在阅读我的算法之前,请注意我没有检查 for 循环中的条件是否正常工作,这取决于您。 However, if they are alright, it should work perfectly.但是,如果它们没问题,它应该可以完美运行。
Map<String, Map<String, String>> result = Maps.newHashMap();
for (Map.Entry<String, Map<String, String>> entry : myMap.entrySet()) {
Map<String, String> handlerMap = Maps.newHashMap();
handlerMap.put("handler", entry.getKey());
boolean save = false;
for (Map.Entry<String, String> entry1 : entry.getValue().entrySet())
if (entry1.getKey().equals("tenant")){
if (entry1.getValue().equals(tenant){
save = true;
}
}
handlerMap.put(entry1.getKey(), entry1.getValue());
}
if (save){
result.put("tenant", handlerMap);
breaK; // Since there is just 1 tenant x handler, I stop the loop
}
}
Please check the below code请检查以下代码
public Map<String, Map<String, String>> getIoiProperties(String tenant)
{
Map<String, String> tenantMap = new HashMap<>();
tenantMap.put("tenant", "ABC");
tenantMap.put("uname", "jk");
tenantMap.put("fname", "ck");
//Map of Maps
Map<String, Map<String, String>> myMap =new HashMap<>();
myMap.put("cfds1", tenantMap);
Map<String, String> tenantMap1 = new HashMap<>();
tenantMap1.put("tenant", "ABC1");
tenantMap1.put("uname", "jk1");
tenantMap1.put("fname", "ck1");
myMap.put("cfds2", tenantMap1);
System.out.println("myMap = " + myMap);
Map<String, Map<String, String>> result = new HashMap<>();
//iterate map
for (Map.Entry<String, Map<String, String>> entry : myMap.entrySet()) {
String key = entry.getKey();
for (Map.Entry<String, String> entry1 : entry.getValue().entrySet())
if (entry1.getKey().equals("tenant") && entry1.getValue().equals(tenant)) {
entry.getValue().put("handler", key);
result.put(entry1.getValue(), entry.getValue());
result.get(tenant).remove("tenant");
}
}
System.out.println(result);
//System.out.println("myMap = " + myMap);
return result;
}
Two issues were there in your code您的代码中有两个问题
"tenant" key not removed from the inner map “租户”键未从内部映射中删除
Instead of result.put(entry1.getKey(), entry.getValue());
而不是result.put(entry1.getKey(), entry.getValue());
result.put(entry1.getValue(), entry.getValue());
should be used.应该使用。
As you said, the previous code by dassum worked.正如您所说, dassum 之前的代码有效。 Based on that, I decided to improve it to try to make it work.基于此,我决定对其进行改进以使其发挥作用。 So, what is happening is that entry.getValue().put("handler", key);
所以,发生的事情是entry.getValue().put("handler", key);
cannot be done while iterating.迭代时无法完成。 You can't modify the entry, as another thread is iterating over it;您不能修改该条目,因为另一个线程正在对其进行迭代; which throws the ConcurrentModificationException .抛出ConcurrentModificationException 。 So here's a way to solve it, I reused some of the code proportioned by erickson .所以这里有一个解决它的方法,我重用了一些由erickson成比例的代码。 What I did is create another map outside the loop to store the data that will be returned.我所做的是在循环外创建另一个映射来存储将返回的数据。 Then, I merge both the previous and stored data.然后,我合并以前的和存储的数据。 I hope this time it works.我希望这次它有效。
public Map<String, Map<String, String>> getIoiProperties(String tenant)
{
Map<String, String> tenantMap = new HashMap<>();
tenantMap.put("tenant", "ABC");
tenantMap.put("uname", "jk");
tenantMap.put("fname", "ck");
//Map of Maps
Map<String, Map<String, String>> myMap =new HashMap<>();
Map<String, Map<String, String>> store =new HashMap<>();
myMap.put("cfds1", tenantMap);
Map<String, String> tenantMap1 = new HashMap<>();
tenantMap1.put("tenant", "ABC1");
tenantMap1.put("uname", "jk1");
tenantMap1.put("fname", "ck1");
myMap.put("cfds2", tenantMap1);
System.out.println("myMap = " + myMap);
Map<String, Map<String, String>> result = new HashMap<>();
//iterate map
for (Map.Entry<String, Map<String, String>> entry : myMap.entrySet()) {
String key = entry.getKey();
for (Map.Entry<String, String> entry1 : entry.getValue().entrySet())
if (entry1.getKey().equals("tenant") && entry1.getValue().equals(tenant)) {
store.put("handler", key);
result.put(entry1.getValue(), entry.getValue());
result.get(tenant).remove("tenant");
}
}
Map<String, Map<String, String>> tmp =new HashMap<>(store);
tmp.keySet().removeAll(result.keySet());
result.putAll(tmp);
store.forEach(target::putIfAbsent);
System.out.println(result);
//System.out.println("myMap = " + myMap);
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.