Guest User

Untitled

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