title: Java HashSet contains function not working
body: <p>I am writing a simple program as follow: Given two numbers M and N, p is from [M,N] and q is from [1,p-1], find all irreducible fractions of p/q.
  My idea is brute force all possible value of p, q. And using HashSet to avoid duplicated fraction. However, somehow the contains function not working as expected. </p>
  
  <p>My code</p>
  
  <pre><code>import java.util.HashSet;
  import java.util.Set;
  
  public class Fraction {
      private int p;
      private int q;
  
      Fraction(int p, int q) {
          this.p = p;
          this.q = q;
      }
  
      public static int getGCD(int a, int b) {
          if (b == 0)
              return a;
          else 
              return getGCD(b, a % b);
      }
  
      public static Fraction reduce(Fraction f) {
          int c = getGCD(f.p, f.q);
          return new Fraction(f.p / c, f.q / c);
      }
  
      public static HashSet&lt;Fraction&gt; getAll(int m, int n) {
          HashSet&lt;Fraction&gt; res = new HashSet&lt;Fraction&gt;();
          for (int p = m; p &lt;= n; p++)
              for (int q = 1; q &lt; p; q++) {
                  Fraction f = new Fraction(p,q);
                  Fraction fr = reduce(f);
                  if (!res.contains(fr))
                      res.add(fr);
              }
          return res;
      }
  
      public static void print(Fraction f) {
          System.out.println(f.p + "/" + f.q);
      }
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          HashSet&lt;Fraction&gt; res = getAll(2, 4);
          for (Fraction f : res)
              print(f);
      }
  
  }
  </code></pre>
  
  <p>Here is the output of program</p>
  
  <pre><code>4/3
  3/1
  4/1
  2/1
  3/2
  2/1
  </code></pre>
  
  <p>you can see the fraction 2/1 is duplicated. Anyone can help me figure out why and how to fix it.
  Many thanks.</p>