简体   繁体   English

Hashmap 相同值不同键比较

[英]Hashmap same values different key comparison

My HashMap of type contains我的 HashMap 类型包含

    String storeId = "3501";
    HashMap<String,String> hMap =  new HashMap<>();
    hMap.put("01",105);
    hMap.put("02",3501);
    hMap.put("07",3501);

    for (int mainLoop=0; mainLoop < 3 ;mainLoop++){
    for (Map.Entry<String, String> map : hMap.entrySet()) {
                    if (storeId.equalsIgnoreCase(map.getValue())) {
                        fulFillmentType = map.getKey();
                    }
                }
    }

Each time mainLopp is executed.每次执行 mainLopp 时。 When it hits "3501" first time only "02" should be return and hitting "3501" on third loop should return "07".当它第一次点击“3501”时,应该只返回“02”,而在第三次循环中点击“3501”应该返回“07”。 Currently output is only "07"目前输出只有“07”

I suggest that the issue is that you are not keeping track of state for all matching keys.我建议问题在于您没有跟踪所有匹配键的状态。 If you want to keep track of all matching keys, then consider using a collection of strings:如果要跟踪所有匹配的键,请考虑使用字符串集合:

String storeId = "3501";
HashMap<String,String> hMap =  new HashMap<>();
hMap.put("01", "105");
hMap.put("02", "3501");
hMap.put("07", "3501");

List<String> matches = new ArrayList<>();

for (int mainLoop=0; mainLoop < 3 ;mainLoop++) {
    for (Map.Entry<String, String> map : hMap.entrySet()) {
        if (storeId.equalsIgnoreCase(map.getValue())) {
            matches.add(map.getKey());
        }
    }
}

matches.forEach(System.out::println);

Note that in your original question, the values of the hMap were integer literals, not strings.请注意,在您的原始问题中, hMap的值是整数文字,而不是字符串。 They would need to be strings enclosed in double quotes in order for your code to even compile.它们需要是用双引号括起来的字符串,以便您的代码甚至可以编译。

You say你说

"02" shoud be return “02”应该回归
third loop should return "07"第三个循环应该返回“07”

but actually you never "return" nor break any loop.但实际上你永远不会“返回”也不会打破任何循环。 So the first time you find 3501, you assign "02" to fulFillmentType and the 2nd time, you replace it with "07".因此,第一次找到 3501 时,将“02”分配给fulFillmentType ,第二次将其替换为“07”。
So after all you iterations : fulFillmentType == "07"所以在你所有的迭代之后: fulFillmentType == "07"

Now, you must know that a HashMap and its entrySet do not guarantee any sort order when you read their content.现在,您必须知道HashMap及其entrySet在您阅读其内容时不保证任何排序顺序。 So, the last read value could randomly be "02" or "07"因此,最后读取的值可能随机为“02”或“07”

Because your this code:因为你的这段代码:

hMap.put("02",3501);

hMap.put("07",3501);

They will Conflict if they Simultaneous Operation They send result as 02or 07 but they Condition is not the same as other如果它们同时操作,它们将发生冲突 它们发送的结果为 02 或 07,但它们的条件与其他条件不同

Your given value matched with key - 02 & 07您给定的值与键匹配 - 02 & 07

So I don't think we need to make a list or anything, I think its a very simple logic to key compression with same values.所以我认为我们不需要制作列表或任何东西,我认为使用相同的值进行密钥压缩是一个非常简单的逻辑。

    String storeId = "3501";
    HashMap<String, String> hMap = new HashMap<>();
    hMap.put("01", "105");
    hMap.put("02", "3501");
    hMap.put("07", "3501");

    for (Map.Entry<String, String> map : hMap.entrySet()) {
        if (storeId.equalsIgnoreCase(map.getValue())) {
            System.out.println("Match Found with Key " + map.getKey());
        }
    }

To get result from first matching you must break;要从第一次匹配中获得结果,您必须break; the loop.循环。 If you want to get keys with the same value you will need to rember result into new ArrayList<String>() .如果您想获得具有相同值的键,您需要将结果重新存储到new ArrayList<String>() With Java 8:使用 Java 8:

List<String> result = map.entrySet().stream().filter(entry -> "someString".equals(entry.getValue())).map(
            Map.Entry::getValue).collect(Collectors.toList());

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

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