简体   繁体   中英

Equals method is not working for two same object

I have written a code to find unique Images. Two images are equal if they have the same name (even if the extension is different) and the size is equal (width * length). But It's failing to find unique images. Even after overriding the equals method, the HashSet method is failing to identify two similar objects.

    import java.util.*;

    class UniqueImages {
    public static class Image {
    private String filename;
    private int width;
    private int height;
    public Image(String filename, int width, int height) {
        this.filename = filename;
        this.width = width;
        this.height = height;
    }
    
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((filename == null) ? 0 : filename.hashCode());
        result = prime * result + height;
        result = prime * result + width;
        return result;
    }

    /**
     * Two Images are considered equal if they have
     * the same filename (without the extension), and the
     * same number of pixels.
     * Thus, flag.jpg with width=60 height=40 is
     * equal to flag.gif with width=40 and height=60
     */
    public boolean equals(Object other) {
        Image o = (Image)other;
        if (filename == null || o.filename == null)
            return false;
        String[] components = filename.split("\\.");
        String[] ocomponents = o.filename.split("\\.");
        return components[0].equals(ocomponents[0]) && 
            width * height == o.width * o.height;
      }

        public String toString() {
           return "Image: filename=" + filename + " Size=" + width*height;
      }
    }

    public static void printImages(Set<Image> images) {
        for(Image image: images) {
           System.out.println(image);
      }
    }

    public static void main(String[] args) {
    Image[] images = {new Image("flag.jpg", 40, 60),
                      new Image("flag.gif", 40, 60),
                      new Image("smile.gif", 100, 200),
                      new Image("smile.gif", 50, 400),
                      new Image("other.jpg", 40, 60),
                      new Image("lenna.jpg", 512, 512),
                      new Image("Lenna.jpg", 512, 512)};
    
          Set<Image> set = new HashSet<Image>(Arrays.asList(images));
          UniqueImages.printImages(set);
        }
      }

If two images having the same total size are considered equal by your equals() method (even if they don't have the same width and height ), they should also have the same hashCode() .

But that's not the only issue. You should also change hashCode() to ignore the file name suffixes, in order for it to fit the implementation of equals() .

public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((filename == null) ? 0 : filename.split("\\.")[0].hashCode());
    result = prime * result + (height * width);
    return result;
}

With these two changes, the HashSet will eliminate two duplicates, resulting in:

Image: filename=smile.gif Size=20000
Image: filename=flag.jpg Size=2400
Image: filename=lenna.jpg Size=262144
Image: filename=other.jpg Size=2400
Image: filename=Lenna.jpg Size=262144

Eran already gave the answer. However, I would like to highlight the issues in your .equals() implementation. Your cast is not safe. Here's how I would write it (using the template given in Bloch's Effective Java ):

public boolean equals(Object o) {
    if(o == this) {
        return true;
    }
    if(!(o instance of Image)) {
        return false;
    }
    Image o = (Image)other;
    if (filename == null || o.filename == null)
        return false;
    String[] components = filename.split("\\.");
    String[] ocomponents = o.filename.split("\\.");
    return components[0].equals(ocomponents[0]) && 
        width * height == o.width * o.height;
}

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