简体   繁体   中英

Hashmap same values different key comparison

My HashMap of type contains

    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. When it hits "3501" first time only "02" should be return and hitting "3501" on third loop should return "07". Currently output is only "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. They would need to be strings enclosed in double quotes in order for your code to even compile.

You say

"02" shoud be return
third loop should return "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".
So after all you iterations : fulFillmentType == "07"

Now, you must know that a HashMap and its entrySet do not guarantee any sort order when you read their content. So, the last read value could randomly be "02" or "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

Your given value matched with key - 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; the loop. If you want to get keys with the same value you will need to rember result into new ArrayList<String>() . With Java 8:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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