简体   繁体   English

识别HashSet中的重复项

[英]Recognizing duplicates in HashSet

So, I have this code written in Java: 因此,我将这段代码用Java编写:

import java.util.HashSet;

class Interval{
  long from;
  long to;

  public Interval(long from, long to) {
    this.from = from;
    this.to = to;
  }

  public boolean equals(Interval other) {

    return from == other.from && to == other.to;
  }


 }

public class Test {

   public static void main(String[] args) {

       HashSet<Interval> mySet  = new HashSet<Interval>();

       mySet.add(new Interval(1,2));
       mySet.add(new Interval(1,2));

       for(Interval in : mySet) {
        System.out.println(in.from + " " + in.to);
       }
   }

 }

The problem is that the set doesn't recognize that there is already an interval from 1 to 2. I defined the function equals, but still it doesn't work. 问题在于集合无法识别出从1到2的间隔。我定义了功能equals,但仍然无法正常工作。 I tried implementing the Comparable interface and overloading the compareTo function, but again nothing. 我尝试实现Comparable接口并重载compareTo函数,但还是没有。 Can somebody tell me how can I solve this problem? 有人可以告诉我如何解决这个问题吗?

Thank you! 谢谢!

You need to override equals from java.lang.Object . 您需要从java.lang.Object重写equals

You did not as yours does not accept Object as parameter. 您没有,因为您不接受Object作为参数。

public boolean equals(Object obj) {
    if (obj == null)
        return false;
    else if (this.getClass() != obj.getClass())
        return false;
    else {
        Interval other = (Interval) obj;
        return from == other.from && to == other.to;
    }
}

For hashCode, you can do this for example. 对于hashCode,您可以例如执行此操作。

public int hashCode() {
    return new Long(this.from).hashCode();
}

So overall you get this code. 因此,总体而言,您将获得此代码。

import java.util.HashSet;

class Interval {
    long from;
    long to;

    public Interval(long from, long to) {
        this.from = from;
        this.to = to;
    }

    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        else if (this.getClass() != obj.getClass())
            return false;
        else {
            Interval other = (Interval) obj;
            return from == other.from && to == other.to;
        }
    }

    public int hashCode() {
        return new Long(this.from).hashCode();
    }
}

public class Test003 {

    public static void main(String[] args) {

        HashSet<Interval> mySet = new HashSet<Interval>();

        mySet.add(new Interval(1, 2));
        mySet.add(new Interval2(1, 2));

        for (Interval in : mySet) {
            System.out.println(in.from + " " + in.to);
        }
    }

}

Use equals and hashCode methods like below it will work perfectly alright 使用下面的equals和hashCode方法将可以正常工作

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + (int) (from ^ from >>> 32);
    result = prime * result + (int) (to ^ to >>> 32);
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    Interval other = (Interval) obj;
    if (from != other.from) {
        return false;
    }
    if (to != other.to) {
        return false;
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM