Java Object instanceof keyword exercise: judge whether the set of the same person is sorted by age. If the same age is sorted by name in alphabetical order, Comparator,
Package com. swift; public class Same_Person_Test {public static void main (String [] args) {/** Object determines whether the Object is the same Person */Person per1 = new Person ("zhangsan", 30 ); person per2 = new Person ("lisi", 27); Person per3 = new Person ("lisi", 27); System. out. println (per3.equals (per2); System. out. println (per1.equals (per2); System. out. println (per2.equals (per2) ;}} class Person {private String name; private int age; public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public Person (String name, int age) {super (); this. name = name; this. age = age;} public boolean equals (Object obj) {if (this = obj) {return true;} if (obj instanceof Person) {Person per = (Person) obj; return this. getName (). equals (per. getName () & this. getAge () = per. getAge () ;}return false ;}}
Sort people by age in ascending order. If the age is the same, sort by name and alphabet in ascending order.
Package com. swift; import java. util. arrayList; import java. util. collections; import java. util. comparator; import java. util. list; import java. util. listIterator; public class Same_Person_Test {public static void main (String [] args) {/** Object determines whether a set of people is sorted by age, if the age is the same, the name is alphabetically ascending */Person [] per = new Person [5]; per [1] = new Person ("zhangsan", 30 ); per [2] = new Person ("lisi", 27); per [3] = new Person ("wangwu", 19); Per [4] = new Person ("wangliu", 19); per [0] = new Person ("tianqi", 37 ); list <Person> list = new ArrayList <Person> (); for (Person person: per) {list. add (person);} System. out. println (per [3]. equals (per [2]); System. out. println (per [1]. equals (per [2]); System. out. println (per [2]. equals (per [2]); Collections. sort (list, new Comparator <Person> () {@ Override public int compare (Person arg0, Person arg1) {int num = Rg0.getAge ()-arg1.getAge (); return num = 0? Arg0.getName (). compareTo (arg1.getName (): num ;}}); ListIterator it = list. listIterator (); while (it. hasNext () {Person p = (Person) it. next (); System. out. println (p. toString () ;}} class Person {private String name; private int age; public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public Person (String name, int age) {super (); this. name = name; this. age = age;} public boolean equals (Object obj) {if (this = obj) {return true;} if (obj instanceof Person) {Person per = (Person) obj; return this. getName (). equals (per. getName () & this. getAge () = per. getAge () ;}return false ;}@ Override public String toString () {return "Person [name =" + name + ", age = "+ age +"] ";}}