hashSet中的重复值

时间:2015-03-10 19:44:20

标签: java equals hashcode hashset

我试图计算总和为n的1,5,10和25的组合数。鉴于我不想重复(例如1 + 5 = 6和5 + 1 = 6)。我使用的是hashSet。我实现了一个名为ResultSet的类,它在解决方案中保存了1,5,10和25的数量,并且我覆盖了equals方法。但是,出于某种原因,我的解决方案hashSet不断返回重复值。为什么呢?

import java.util.HashSet;

public class Solution {

  public static void main(String[] args) {
    int N = 6;
    int combinationsSolution = new Combine(N).getSolution();
    System.out.println("N= " + N + " Number of solutions= " + combinationsSolution);

  }
}

class Combine {

  private int solution;

  private int n;

  private HashSet<ResultSet> cacheUnordered = new HashSet<ResultSet>();

  public Combine(int N) {
    this.n = N;
    this.solution = solve(n);
  }

  public int getSolution() {
    return solution;
  }

  public int solve(int N) {
    solve(N, 0, 0, 0, 0);
    for (ResultSet r:cacheUnordered){
      System.out.println(r.toString());
    }
    return cacheUnordered.size();
  }

  public void solve(int N, int substracted1, int substracted5, int substracted10, int substracted25) {
    if (N == 0) {
      cacheUnordered.add(new ResultSet(substracted1, substracted5, substracted10, substracted25));
    } else if (N > 0) {
      solve(N - 1, substracted1 + 1, substracted5, substracted10, substracted25);
      solve(N - 5, substracted1, substracted5 + 1, substracted10, substracted25);
      solve(N - 10, substracted1, substracted5, substracted10 + 1, substracted25);
      solve(N - 25, substracted1, substracted5, substracted10, substracted25 + 1);
    }
  }
}

class ResultSet {
  private int numberOf1;

  private int numberOf5;

  private int numberOf10;

  private int numberOf25;

  public ResultSet(int num1, int num5, int num10, int num25) {
    numberOf1 = num1;
    numberOf5 = num5;
    numberOf10 = num10;
    numberOf25 = num25;
  }

  @Override
  public String toString(){
    String result;
    result = numberOf1 + " " + numberOf5 + " " + numberOf10 + " " + numberOf25;
    return result;
  }

  @Override
  public boolean equals(Object r2) {
    if (r2 == null) {
      return false;
    }
    if (!(r2 instanceof ResultSet)) {
      return false;
    }
    ResultSet rr = (ResultSet) r2;
    if (rr.numberOf1 == this.numberOf1 && rr.numberOf5 == this.numberOf5
            && rr.numberOf10 == this.numberOf10 && rr.numberOf25 == this.numberOf25) {
      System.out.println("Comparing " + this.toString() + " to " + rr.toString());
      return true;
    } else {
      return false;
    }
  }

  public int getNum1() {
    return numberOf1;
  }

  public int getNum5() {
    return numberOf5;
  }

  public int getNum10() {
    return numberOf10;
  }

  public int getNum25() {
    return numberOf25;
  }
}

2 个答案:

答案 0 :(得分:5)

对于您的ResultSet课程,您定义了equals()方法,但未定义hashCode()方法。您需要两种方法HashSet才能正常工作。请参阅此explanation。 (它讨论HashMap,但它也适用于HashSet。)

答案 1 :(得分:3)

作为JavaDoc明确指定

  

请注意,通常需要覆盖hashCode方法   每当重写此方法时,都要保持一般   hashCode方法的契约,它声明了相等的对象必须   有相同的哈希码。

你没有遵循它,这就是你得到重复的原因,

Please Read How HashCode and Equals Work它会帮助您更好地理解上述陈述