fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Ideone {
  8.  
  9. static class X implements Comparable<X> {
  10. String campo1;
  11.  
  12. X(String campo1) {
  13. this.campo1 = campo1;
  14. }
  15.  
  16. @Override
  17. public int hashCode() {
  18. final int prime = 31;
  19. int result = 1;
  20. result = prime * result + ((campo1 == null) ? 0 : campo1.hashCode());
  21. return result;
  22. }
  23.  
  24. @Override
  25. public boolean equals(Object obj) {
  26. if (this == obj)
  27. return true;
  28. if (obj == null)
  29. return false;
  30. if (getClass() != obj.getClass())
  31. return false;
  32. X other = (X) obj;
  33. if (campo1 == null) {
  34. if (other.campo1 != null)
  35. return false;
  36. } else if (!campo1.equals(other.campo1))
  37. return false;
  38. return true;
  39. }
  40.  
  41. @Override
  42. public int compareTo(X o) {
  43. return campo1.compareTo(o.campo1);
  44. }
  45.  
  46. }
  47.  
  48. public static void main(String[] args) {
  49.  
  50. X x1 = new X("A");
  51. X x2 = new X("B");
  52. X x3 = new X("C");
  53. Set<X> ts = new TreeSet<X>();
  54. ts.add(x2);
  55. ts.add(x3);
  56. System.out.println(ts.contains(x1));
  57.  
  58. }
  59.  
  60. }
  61.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
false