如何在Java中比较哈希映射?

huangapple go评论97阅读模式
英文:

how to compare hashmap in Java?

问题

我有两个HashMaps,我想比较值的键,如果它们不同,就返回两个键之间的差异。

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);
	}
}
// 输出结果
// inputLocation = {0=72, 1=101, 2=108, 3=108, 4=111}
// input1Location = {0=101, 1=72, 2=108, 3=108, 4=111}

示例

这里

72的键在inputLocation中是0,但在input1Location中是1

101的键在inputLocation中是1,但在input1Location中是0

因此输出应为2(即更改次数)

英文:

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 )

答案1

得分: 1

如果需要计算已更改的值的数量,此代码应该有效。

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将给您更改的值的计数。

这只是一些初步的代码,请随时更新它。我建议您使用HashMap<Integer, Integer>而不是普通的HashMap,因为它可以保证类型安全。

英文:

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.

答案2

得分: 0

你必须在两个对象HashMap的keySet()上使用equals来比较单个值。通过这样做,你可以比较/获取哈希的键参数。然后,你必须获取HashMap的值来比较数值。但是,如果你需要比较整个哈希,只需使用equals。

你的代码将会如下所示:

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<Integer, Integer> inputLocation = obj1.getCharacterLocation(input);
        HashMap<Integer, Integer> input1Location = obj1.getCharacterLocation(input1);
        System.out.println(inputLocation);
        System.out.println(input1Location);

        // 这是通过键比较值
        inputLocation.get(2).equals(input1Location.get(2));
        // 2是HashMap中键值的第二个参数

        // 这是比较两个哈希
        inputLocation.equals(input1Location);

        // 打印输出
        // inputLocation = {0=72, 1=101, 2=108, 3=108, 4=111}
        // input1Location = {0=101, 1=72, 2=108, 3=108, 4=111}
        // true
        // false
    }
}

注意:代码中的特殊字符已经被删除,例如"已经被替换为普通的双引号。

英文:

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 = &quot;Hello&quot;;
    String input1 = &quot;eHllo&quot;;
    
    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


}}

huangapple
  • 本文由 发表于 2020年7月22日 21:19:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63035144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定