简体   繁体   English

需要一双崭新的眼睛才能得出在地图比较值背后的逻辑

[英]Need a Fresh pair of Eyes to Work Out the Logic Behind Comparing Values in a Map of Maps

Problem 问题

Data is in the format: 数据格式为:

Map<String, Map<String, Integer>>

Which looks something like this: 看起来像这样:

{"MilkyWay": {"FirstStar" : 3, "SecondStar" : 9 .... }, "Andromeda": {"FirstStar" : 10, "SecondStar" : 9 .... } }

I want to compare the Star values in a quick loop, so I'd like to compare the integer value of FirstStar in MilkyWay and Andromeda and have it return true or false if the values are the same or not. 我想在一个快速循环中比较Star值,所以我想比较MilkyWayAndromedaFirstStar的整数值,如果值相同或不同,则返回truefalse Since this Map of Maps is huge. 由于此地图地图巨大。

My Attempt 我的尝试

I'd like to do it something like: 我想做这样的事情:

//GalaxyMap<String, <Map<String, Integer>>

            for (Map<String, Integer> _starMap : GalaxyMap.values())
            {
                for (String _starKey : _starMap.keySet()){
                    //Can't quite think of the correct logic... and I'm tired...
                }

            }

I'd like to keep it as short as possible... I've been staring at this for a while and I'm going in circles with it. 我想让它尽可能的短。。。我一直在盯着它看一会儿,现在我正和它一起盘旋。

EDIT 编辑

Outer keys differ, Inner keys are the same 外键不同,内键相同

Also since this is a response from a server, I don't know the size it's going to be 而且由于这是服务器的响应,所以我不知道它的大小

Why does this need to be a map. 为什么这需要成为一张地图。 If you're always using "FirstStar", "SecondStar" etc, as your keys, then why not make it a list instead.. 如果您始终使用“ FirstStar”,“ SecondStar”等作为键,那么为什么不将其作为列表。

Map<String, List<Integer>>

Then you can do something like: 然后,您可以执行以下操作:

public boolean compareGalaxyStar(String galaxyName, String otherGalaxyName, int star) {
    List<Integer> galaxyStars = galaxyMap.get(galaxyName);
    List<Integer> otherGalaxyStars = galaxyMap.get(otherGalaxyName);

    return galaxyStars.get(star) == otherGalaxyStars.get(star);
}

NOTE : You need to do some validation to make sure the input is correct. 注意 :您需要进行一些验证以确保输入正确。

To implement this for all stars, it is not much different. 要为所有星星实施此操作,并没有太大不同。

if(galaxyStars.size() == otherGalaxyStars.size()) {
    for(int x = 0; x < galaxyStars.size(); x++) {
        // Perform your comparisons.
        if(galaxyStars.get(x) != otherGalaxyStars.get(x)) {
             // Uh oh, unequal.
             return false;
        }
    }
}

If the structure of the inner maps also could differ, you should do something like that: 如果内部映射的结构也可能不同,则应执行以下操作:

static boolean allStarValuesEqual(Map<String, Map<String, Integer>> galaxies) {
    Map<String, Integer> refStars = null;
    for (Map<String, Integer> galaxy : galaxies.values()) {
        if (refStars == null) {
            refStars = galaxy;
        } else {
            for (Entry<String, Integer> currentStar : galaxy.entrySet()) {
                if (!currentStar.getValue().equals(refStars.get(currentStar.getKey()))) {
                    return false;
                }
            }
        }
    }
    return true;
}

Please check below program along with output: 请检查以下程序以及输出:

package com.test;

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

public class CompareMapValues {
    private final static String FS = "FirstStar";
    private final static String SS = "SecondStar";
    private final static String MW = "MilkyWay";
    private final static String A = "Andromeda";

    public static void main(String[] args) {
        Map> map = new HashMap>();
        Map innerMap1 = new HashMap();
        innerMap1.put(FS, 3);
        innerMap1.put(SS, 9);
        Map innerMap2 = new HashMap();
        innerMap2.put(FS, 10);
        innerMap2.put(SS, 9);
        map.put(MW, innerMap1);
        map.put(A, innerMap2);
        Set set = map.keySet();
        for(String s: set) {
            Map outerMap = map.get(s);
            Set set2 = map.keySet();
            for(String s2: set2) {
                Map innerMap = map.get(s2);
                if(!s2.equals(s)) {
                    Set set3 = outerMap.keySet();
                    for(String s3: set3) {
                        int i1 = outerMap.get(s3);
                        Set set4 = innerMap.keySet();
                        for(String s4: set4) {
                            int i2 = innerMap.get(s3);
                            if(s3.equals(s4) && i1==i2) {
                                System.out.println("For parent " + s + " for " + s3 + " value is " + i1);
                            }
                        }
                    }
                }
            }
        }
    }
}
//Output:
//For parent Andromeda for SecondStar value is 9
//For parent MilkyWay for SecondStar value is 9

Hope this helps. 希望这可以帮助。

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

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