TransWikia.com

how to compare hashmap in Java?

Stack Overflow Asked on November 24, 2021

I have two HashMaps and i want to compare the keys of the values and if they are different return the difference between both keys

public class AsciiCount {
    public static void main(String args[]) {
        String input = "Hello";
        String input1 = "eHllo";
        
        Store obj1= new Store();
        
        String orderOfString = obj1.CheckOrder(input);
        System.out.println(orderOfString);
        
        HashMap inputLocation = obj1.getCharacterLocation(input);
        HashMap input1Location = obj1.getCharacterLocation(input1);
        System.out.println(inputLocation);
        System.out.println(input1Location);
    }
}
// OUTPUT of print
// inputLocation = {0=72, 1=101, 2=108, 3=108, 4=111}
// input1Location = {0=101, 1=72, 2=108, 3=108, 4=111}

Example

Here

Key of 72 is 0 in inputLocation but key of 72 is 1 in input1Location

Key of 101 is 1 in inputLocation but key of 101 is 0 in input1Location

So output should be 2 ( i.e no. of changes )

2 Answers

If what is needed is the count of values which have changed, this code should work.

HashMap inputLocation = obj1.getCharacterLocation(input);
HashMap input1Location = obj1.getCharacterLocation(input1);
int diffCount = 0;
for (Object key : inputLocation.keySet()) {
     if ( !inputLocation.get(key).equals(input1Location.get(key))){
         diffCount++;           
     }
}

diffCount will give you the count of changed values.

This is just some rough code, please feel free to update it. I advice you to use a HashMap<Integer, Integer> instead of a plain HashMap, as that guarantees type saftey.

Answered by BlackPearl on November 24, 2021

You have to use equals on the keySet() of both objects HashMaps to compare a single value. With this you can compare/obtain the key param of the Hash. After this you have to get the value of the HashMap to compare the value. But if you need to compare the entire hash just use equals.

Your code it will look like this:

public class AsciiCount {
public static void main(String args[]) {
    String input = "Hello";
    String input1 = "eHllo";
    
    Store obj1= new Store();
    
    String orderOfString = obj1.CheckOrder(input);
    System.out.println(orderOfString);
    HashMap inputLocation = obj1.getCharacterLocation(input);
    HashMap input1Location = obj1.getCharacterLocation(input1);
    System.out.println(inputLocation);
    System.out.println(input1Location);
    HashMap input1Location = obj1.getCharacterLocation(input1);

    // This is the comparison of the value from the key
    inputLocation.get(inputLocation.keySet()[2]).equals(input1Location.get(input1Location.keySet()[2]);
    // 2 is the second argument of the key value in the HashMap

    // This is the comparison of the two hashes
    inputLocation.equals(input1Location);

    // OUTPUT of print
    // inputLocation = {0=72, 1=101, 2=108, 3=108, 4=111}
    // input1Location = {0=101, 1=72, 2=108, 3=108, 4=111}
    // true
    // false


}}

Answered by João Jardim on November 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP