Advertisement
apl-mhd

equalsMethod

Dec 13th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package com.company;
  2.  
  3. /**
  4.  * Created by common on 12/13/2017.
  5.  */
  6. public class Person {
  7.  
  8.     String name;
  9.     String ID;
  10.     int age;
  11.  
  12.     public Person(String name, String ID, int age) {
  13.         this.name = name;
  14.         this.ID = ID;
  15.         this.age = age;
  16.     }
  17.  
  18.     public boolean equals(Object o){
  19.  
  20.         if(this == o)
  21.             return true;
  22.  
  23.         if(o.getClass() !=this.getClass()){
  24.  
  25.             return false;
  26.         }
  27.  
  28.         Person p =  (Person)o;
  29.  
  30.         return name.equals(p.name);
  31.     }
  32.  
  33.     public String toString(){
  34.  
  35.  
  36.         return  name+" "+ID+" "+age;
  37.     }
  38.  
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. package com.company;
  49.  
  50. import java.util.ArrayList;
  51.  
  52. public class Main {
  53.  
  54.     public static void main(String[] args) {
  55.     // write your code here
  56.  
  57.         ArrayList<Person> personArrayList =new ArrayList<>();
  58.         Person p1 = new Person("Orko","036",2);
  59.         Person p2 = new Person("ORange","126",20);
  60.         Person p3 = new Person("Apple","236",30);
  61.         Person p4 = new Person("Orko","536",50);
  62.  
  63.         personArrayList.add(p1);
  64.         personArrayList.add(p2);
  65.         personArrayList.add(p3);
  66.         personArrayList.add(p4);
  67.  
  68.  
  69.       //  personArrayList.remove(p1);
  70.        // personArrayList.clear();
  71.  
  72.  
  73.  
  74.         for(Person a:personArrayList)
  75.             System.out.println(a);
  76.  
  77.     }
  78. }
  79.  
  80.  
  81. /*
  82.  public int compareTo(Person o) {
  83.  
  84.  
  85.         return o.name.compareTo(this.name);
  86.     }
  87. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement