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. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static class Test {
  11.  
  12. private String name;
  13.  
  14. public Test(String name) {
  15. this.name = name;
  16. }
  17.  
  18. public String GetName() { return name; }
  19. public void SetName(String name) { this.name = name; }
  20.  
  21. @Override
  22. public boolean equals(Object o) {
  23. if (o == null) return false;
  24. if (o == this) return true;
  25. if (!(o instanceof Test)) {
  26. return (o instanceof String) && (name.equals(o));
  27. }
  28. Test t = (Test)o;
  29. return name.equals(t.GetName());
  30. }
  31.  
  32. @Override
  33. public int hashCode() {
  34. return name.hashCode();
  35. }
  36.  
  37. @Override
  38. public String toString() {
  39. return name;
  40. }
  41.  
  42. }
  43.  
  44. public static void main (String[] args) throws java.lang.Exception
  45. {
  46. String x = "a";
  47.  
  48. Test a = new Test(x);
  49. Test b = new Test(x);
  50. System.out.println(a.equals(b));
  51. System.out.println(a.equals(x));
  52.  
  53. List<Test> test = new ArrayList<Test>();
  54. test.add(a);
  55. test.add(b);
  56.  
  57. System.out.println(test.contains(new Test(x)));
  58.  
  59. }
  60. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
true
true
true