Guest User

Untitled

a guest
Jun 25th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. public class Person implements Comparable<Person>
  2. {
  3.  
  4. String name;
  5. int age;
  6. String mail;
  7.  
  8. public String getMail() {
  9. return mail;
  10. }
  11.  
  12. public void setMail(String mail) {
  13. this.mail = mail;
  14. }
  15.  
  16. public Person(String name, int age, String mail) {
  17. this.name = name;
  18. this.age = age;
  19. }
  20.  
  21. public String getName() {
  22. return name;
  23. }
  24.  
  25. public int getAge() {
  26. return age;
  27. }
  28.  
  29. public String toString() {
  30. return name + " : " + age;
  31. }
  32.  
  33. public int compareTo(Person p) {
  34. return getMail().compareTo(p.getMail());
  35. }
  36. // wants to re-write this method
  37. @Override
  38. public boolean equals(Object obj) {
  39. if (this == obj) {
  40. return true;
  41. }
  42. if (obj == null) {
  43. return false;
  44. }
  45. if (getClass() != obj.getClass()) {
  46. return false;
  47. }
  48. return false;
  49. }
  50.  
  51. public static void main(String[] args) {
  52. List<Person> people = new ArrayList<Person>();
  53. people.add(new Person("Homer", 38, "shankar@gmail.com"));
  54. people.add(new Person("Marge", 35, "shankar6@gmail.com"));
  55. people.add(new Person("Bart", 15, "ramr@gmail.com"));
  56. people.add(new Person("Lisa", 13, "ramkumar@gmail.com"));
  57.  
  58. /*
  59. * Collections.sort(people, new Person.AgeComparator());
  60. * System.out.println("Sort using Age Comparator");
  61. */
  62. System.out.println("t" + people);
  63.  
  64. List<Person> people1 = new ArrayList<Person>();
  65. people1.add(new Person("jug", 38, "jug@gmail.com"));
  66. people1.add(new Person("benny", 35, "benny@gmail.com"));
  67. people1.add(new Person("Bart", 15, "ramr@gmail.com"));
  68. people1.add(new Person("Lisa", 13, "ramkumar@gmail.com"));
  69.  
  70. for (Person people: people) {
  71. // WIHTHOUT ITERATING people1 i want to compare against people object
  72. if(people==people1)
  73. {
  74. System.out.println(" matched");
  75. System.out.println(people);
  76. }
  77. else
  78. {
  79. System.out.println("not matched");
  80. System.out.println(people);
  81. }
  82. }
  83.  
  84. }
  85.  
  86. matched
  87. Bart : 15 :ramr@gmail.com
Add Comment
Please, Sign In to add comment