Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person implements Comparable<Person>
- {
- String name;
- int age;
- String mail;
- public String getMail() {
- return mail;
- }
- public void setMail(String mail) {
- this.mail = mail;
- }
- public Person(String name, int age, String mail) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- public String toString() {
- return name + " : " + age;
- }
- public int compareTo(Person p) {
- return getMail().compareTo(p.getMail());
- }
- // wants to re-write this method
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- return false;
- }
- public static void main(String[] args) {
- List<Person> people = new ArrayList<Person>();
- people.add(new Person("Homer", 38, "shankar@gmail.com"));
- people.add(new Person("Marge", 35, "shankar6@gmail.com"));
- people.add(new Person("Bart", 15, "ramr@gmail.com"));
- people.add(new Person("Lisa", 13, "ramkumar@gmail.com"));
- /*
- * Collections.sort(people, new Person.AgeComparator());
- * System.out.println("Sort using Age Comparator");
- */
- System.out.println("t" + people);
- List<Person> people1 = new ArrayList<Person>();
- people1.add(new Person("jug", 38, "jug@gmail.com"));
- people1.add(new Person("benny", 35, "benny@gmail.com"));
- people1.add(new Person("Bart", 15, "ramr@gmail.com"));
- people1.add(new Person("Lisa", 13, "ramkumar@gmail.com"));
- for (Person people: people) {
- // WIHTHOUT ITERATING people1 i want to compare against people object
- if(people==people1)
- {
- System.out.println(" matched");
- System.out.println(people);
- }
- else
- {
- System.out.println("not matched");
- System.out.println(people);
- }
- }
- }
- matched
- Bart : 15 :ramr@gmail.com
Add Comment
Please, Sign In to add comment