fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main (String[] args) {
  5. Set set = new HashSet();
  6. Data d = new Data(1,"foo");
  7. set.add(d);
  8. set.add(new Data(2,"bar"));
  9. System.out.println(set.contains(d));
  10. d.field1 = 2;
  11. System.out.println(set.contains(d));
  12. }
  13.  
  14. public static class Data {
  15. int field1;
  16. String field2;
  17.  
  18. Data(int f1, String f2) {
  19. field1 = f1;
  20. field2 = f2;
  21. }
  22.  
  23. public int hashCode() {
  24. return field2.hashCode() + field1;
  25. }
  26.  
  27. public boolean equals(Object o) {
  28. if(!(o instanceof Data)) return false;
  29. Data od = (Data)o;
  30. return od.field1 == this.field1 && od.field2.equals(this.field2);
  31.  
  32. }
  33. }
  34. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
true
false