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,

Source: Internet
Author: User
Tags sorted by name

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 +"] ";}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.