fork download
  1. import java.util.HashSet;
  2.  
  3. final class Connection {
  4. public final int A;
  5. public final int B;
  6.  
  7. public Connection(final int a, final int b) {
  8. if (a == b)
  9. throw new IllegalArgumentException("points must be different");
  10. A = a;
  11. B = b;
  12. }
  13.  
  14. @Override
  15. public boolean equals(Object o) {
  16. if (this == o) return true;
  17. if (o == null || getClass() != o.getClass()) return false;
  18.  
  19. Connection that = (Connection) o;
  20.  
  21. return (A == that.A && B == that.B || A == that.B && B == that.A);
  22. }
  23.  
  24. @Override
  25. public int hashCode() {
  26. return A ^ B;
  27. }
  28.  
  29. @Override
  30. public String toString() {
  31. return "Connection{" +
  32. "A=" + A +
  33. ", B=" + B +
  34. '}';
  35. }
  36. }
  37.  
  38.  
  39. class Ideone {
  40. public static void main(String[] args) throws java.lang.Exception {
  41. HashSet<Connection> s = new HashSet<>();
  42. s.add(new Connection(1, 3));
  43. s.add(new Connection(2, 3));
  44. s.add(new Connection(3, 2));
  45. s.add(new Connection(1, 3));
  46. s.add(new Connection(2, 1));
  47.  
  48. s.remove(new Connection(1, 2));
  49.  
  50. for (Connection x : s) {
  51. System.out.println(x);
  52. }
  53.  
  54. // output:
  55. // Connection{A=2, B=3}
  56. // Connection{A=1, B=3}
  57. }
  58. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
Connection{A=2, B=3}
Connection{A=1, B=3}