Compare two hashmaps

Asked

Viewed 69 times

1

Hello, I need to buy the values of 2 Hashmaps. I’m making an Image Matcher using equal amount of colors, using this code, but I don’t know how I can compare them.

public static double DoesMatch2(BufferedImage arg0, BufferedImage arg1){
    HashMap<Color, Integer> pixMap0 = new HashMap<>();
    HashMap<Color, Integer> pixMap1 = new HashMap<>();
    for (int x = arg0.getWidth()-1; x >= 0; x--) {
        for (int y = arg0.getHeight()-1; y >= 0; y--) {
            int clr=  arg0.getRGB(x,y); 
            int  red   = (clr & 0x00ff0000) >> 16;
            int  green = (clr & 0x0000ff00) >> 8;
            int  blue  =  clr & 0x000000ff;
            Color color = new Color(red, green, blue);
            if(pixMap0.containsKey(color)){
                pixMap0.put(color, pixMap0.get(color)+1);
            }else{
                pixMap0.put(color, 1);
            }
        }
    }
    for (int x = arg1.getWidth()-1; x >= 0; x--) {
        for (int y = arg1.getHeight()-1; y >= 0; y--) {
            int clr=  arg1.getRGB(x,y); 
            int  red   = (clr & 0x00ff0000) >> 16;
            int  green = (clr & 0x0000ff00) >> 8;
            int  blue  =  clr & 0x000000ff;
            Color color = new Color(red, green, blue);
            if(pixMap1.containsKey(color)){
                pixMap1.put(color, pixMap0.get(color)+1);
            }else{
                pixMap1.put(color, 1);
            }
        }
    }
    return 0;
}
  • pixMap1.equals(pixMap0) doesn’t work?

  • No, I need to see if each value is equal and compare and then add and divide by the total

  • And it should match when you have exactly the same keys with the same values ? Or one may have fewer keys than another as long as they have no different ?

  • 1

    Will the two hashmaps always have the same keys, but with possibly different values? Or can the keys be different too?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.