简体   繁体   English

反转包含另一个嵌套地图的地图

[英]Reverse a map that contains another nested map

i would like to know how can I get a new map that is the reversed map of mine? 我想知道如何获得一张反映我地图的新地图? My actual Map looks like that: 我的实际地图看起来像这样:

centralMap = new HashMap<String, Map<String, String>>();
nestedMap = new HashMap<String, String>();

the nestedMap is just created in the put-method.. and to put an element i use the following in the main method: nestedMap刚刚在put-method中创建..并且放置一个元素我在main方法中使用以下内容:

TrueStringMap2D testmap = new TrueStringMap;
testmap.put("Mickey Mouse","Mathematics","1.0");
testmap.put("Mickey Mous","Physics","1.3");
testmap.put("Minnie","Chemistry","2.3");
......

now i would like to reverse the map through a method that i named "flipped()" i want to change the Keys of the nestedMap to Keys of the centralMap, and vice-versa.. so every "subject" (like mathematics, physics, ..) will have a nestedMap of students and the grades.. how could i do that? 现在我想通过一个名为“flipped()”的方法来反转地图我希望将嵌套地图的键更改为中心地图的键,反之亦然..所以每个“主题”(如数学,物理学) ,..)将有一个学生和成绩的嵌套地图..我怎么能这样做?

im not allowed to create classes in my TrueString2D.. i just need to copy perhaps the Keys of the centralMap in a list, and those of the nestedMap in another List, and then create a new map HashMap>(); 我不允许在我的TrueString2D中创建类..我只需要复制一个列表中的centralMap的键,以及另一个List中的嵌套Map的那些,然后创建一个新的映射HashMap>(); (same as my centralMap) and copy the list of old keys of the nestedMap in the NEW created map (for ex. newCentralMap) as keys, and as value, i'll copy the old keys of the centralMap in the newNestedMap and the values of the newNestedMap are the same as the ones on the old map.. but i dont know exactly how to do that, and if i can copy a list in a map :S (与我的centralMap相同)并将新创建的地图(例如newCentralMap)中嵌套地图的旧键列表复制为键,作为值,我将复制newNestedMap中centralMap的旧键和值newNestedMap的内容与旧地图上的相同..但我不确切知道如何做到这一点,如果我可以复制地图中的列表:S

Thankyou verymuch 非常感谢你

Use something different for storing your items: 使用不同的东西存储您的物品:

  • Guava has a Table class that implements the features you are asking for Guava有一个Table类,它实现了你要求的功能
  • If you need even more flexibility, consider an in-memory database 如果您需要更多灵活性,请考虑使用内存数据库

I wouldn't use maps for this. 我不会为此使用地图。 I would just use a Collection<PersonGrade> . 我只想使用Collection<PersonGrade>

class PersonGrade {
   String name;
   String subject;
   Double grade;
}

Then when you want to generate a report about "All grades of Mickey Mouse on all subjects" iterate over the Collection and take the objects which fits that criteria. 然后,当您想要生成关于“所有主题上的所有成绩的米老鼠”的报告时,迭代收集并获取符合该标准的对象。 I know this could be a bit slow when you are dealing with a huge amount of data. 我知道当你处理大量数据时,这可能会有点慢。 But I would really give it a try. 但我真的试一试。

Why don't you use HashMap<String, Map<String, String>>() instead of TrueStringMap2D 为什么不使用HashMap<String, Map<String, String>>()而不是TrueStringMap2D

import java.util.HashMap;
import java.util.Map;

public class Flip {


    public static Map <String, Map<String, String>> flip(Map <String, Map<String, String>> map){
        Map <String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
        for (String key : map.keySet()){
            for (String key2 : map.get(key).keySet()){
                if (!result.containsKey(key2)){
                    result.put(key2, new HashMap<String, String>());
                }

                result.get(key2).put(key, map.get(key).get(key2));
            }
        }


        return result;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Map <String, Map<String, String>> map = new HashMap<String, Map<String, String>>();

        map.put("Mickey", new HashMap<String, String>());
        map.get("Mickey").put("Physics", "1.1");
        map.get("Mickey").put("Maths", "1.2");

        map.put("Minnie", new HashMap<String, String>());
        map.get("Minnie").put("Physics", "1.1");
        map.get("Minnie").put("Chemistry", "1.3");

        System.out.println(map);

        System.out.println(flip(map));
    }

}

the output 输出

{Minnie={Physics=1.1, Chemistry=1.3}, Mickey={Maths=1.2, Physics=1.1}}
{Maths={Mickey=1.2}, Physics={Minnie=1.1, Mickey=1.1}, Chemistry={Minnie=1.3}}

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

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