Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

391
Views
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);
        }
      }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!