简体   繁体   中英

HashMap Key Comparison and return values in JAVA

I want to compare keys of two different hash maps say

Map<String, Float> map1 = new HashMap<>();
Map<String, Float> map2 = new HashMap<>();

map1:

<org.openjdk.jmh.samples.JMHSortBenchmark.collectionsSort,6691.679>
<org.openjdk.jmh.samples.JMHSortBenchmark.abcdSort,5151.45>
<org.openjdk.jmh.samples.JMHSortBenchmark.saasSort,5454.54>
<org.openjdk.jmh.samples.JMHSortBenchmark.xyzSort,888.22>

map2:

<org.openjdk.jmh.samples.JMHSortBenchmark.xyzSort,7448.362>
<org.openjdk.jmh.samples.JMHSortBenchmark.abcdSort,951.5>
<org.openjdk.jmh.samples.JMHSortBenchmark.lmnSort,4454.54>

And if they match eg., "org.openjdk.jmh.samples.JMHSortBenchmark.xyzSort" so I want to return the <Key,Value> pair of both map1 and map2 ie, it must return

org.openjdk.jmh.samples.JMHSortBenchmark.xyzSort,888.22
org.openjdk.jmh.samples.JMHSortBenchmark.xyzSort,7448.362


org.openjdk.jmh.samples.JMHSortBenchmark.abcdSort,5151.45
org.openjdk.jmh.samples.JMHSortBenchmark.abcdSort,951.5

because I want to process the difference between their values and compare them ie, 888.2 in map1 and 7448.362 in map2 thereby logging the difference to a csv file.

I used the following code:

for (Entry<String, Float> entry: map1.entrySet()) {
    if (map2.containsKey(entry.getKey())) {

        System.out.println("The matched value is" + entry.getValue() +"and Key is"+ entry.getKey());                            

    }
} 

but this could return only the values of map1 and not map2.

I would do it like this:

map1.keySet().retainAll(map2.keySet());

The keySet() method will give you a set view (!) on the keys of the map. retainAll() will only keep the elements in that set that are keys in map2, too. If you want to keep all values of map1 you may want to make a copy first.

I have made a working solution for you.

static void test11()
{
    HashMap<String, Float> map1 = new HashMap<>();
    HashMap<String, Float> map2 = new HashMap<>();

    map1.put("org.openjdk.jmh.samples.JMHSortBenchmark.collectionsSort",(float) 6691.679);
    map1.put("org.openjdk.jmh.samples.JMHSortBenchmark.abcdSort1",(float) 5151.45);
    map1.put("org.openjdk.jmh.samples.JMHSortBenchmark.saasSort",(float) 5454.54);
    map1.put("org.openjdk.jmh.samples.JMHSortBenchmark.xyzSort",(float) 888.22);


    map2.put("org.openjdk.jmh.samples.JMHSortBenchmark.xyzSort", (float) 7448.362);
    map2.put("org.openjdk.jmh.samples.JMHSortBenchmark.abcdSort", (float) 951.5);
    map2.put("org.openjdk.jmh.samples.JMHSortBenchmark.lmnSort", (float) 4454.54);

    for(String key: map1.keySet())
    {
        // use key to search 2nd list, will be null if no matching key found
        Float map2data = map2.get(key);

        if (null == map2data)
        {
            // Current key not found
        }
        else
        {
            Float map1data = map1.get(key);

            // You can do you operations here with matching keys data here
        }
    }
}

Hope this will help. :-)

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