Advertisement
vessos

equality logic

Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. package equalityLogic;
  2.  
  3. public class Person implements Comparable<Person> {
  4. private String name;
  5. private int age;
  6.  
  7. public Person(String name,int age){
  8. this.name = name;
  9. this.age = age;
  10. }
  11.  
  12. @Override
  13. public int hashCode() {
  14. int hash = 1;
  15. hash = hash*17+this.age;
  16. hash = hash *13+this.name.hashCode();
  17. return hash;
  18. }
  19.  
  20. @Override
  21. public boolean equals(Object obj) {
  22. if(obj==null){
  23. return false;
  24. }
  25. if(!(obj instanceof Person)){
  26. return false;
  27. }
  28. Person person = (Person)obj;
  29. if(!this.name.equals(person.name)){
  30. return false;
  31. }
  32. if(this.age!=person.age){
  33. return false;
  34. }
  35. return true;
  36. }
  37.  
  38. @Override
  39. public int compareTo(Person o) {
  40. return 0;
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement