equals方法不适用于两个相同的对象

kmbjn2e3  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(406)

我写了一个代码来寻找独特的图像。如果两个图像具有相同的名称(即使扩展名不同)并且大小相等(宽度*长度),则两个图像相等。但它找不到独特的图像。即使在重写equals方法之后,hashset方法也无法识别两个相似的对象。

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);
        }
      }
nwnhqdif

nwnhqdif1#

如果您认为两个总大小相同的图像相等 equals() 方法(即使它们没有相同的 width 以及 height ),它们也应该具有相同的 hashCode() .
但这不是唯一的问题。你也应该改变 hashCode() 忽略文件名后缀,以使其适合 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;
}

有了这两个变化 HashSet 将消除两个重复项,导致:

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
ewm0tg9j

ewm0tg9j2#

伊兰已经给出了答案。不过,我想强调一下你报告中的问题 .equals() 实施。你的石膏不安全。下面是我将如何编写它(使用bloch的effectivejava中给出的模板):

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;
}

相关问题