Java comparer comparator using samples to share _java

Source: Internet
Author: User

Copy Code code as follows:

Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;
Import java.util.List;

public class Comparatortest implements comparator<stuentity> {

/**
* @param args
*/
public static void Main (string[] args) {
list<stuentity> list = new arraylist<stuentity> ();
Stuentity stud1=new stuentity ();
Stud1.setage (10);
Stud1.setname ("abc");
Stuentity stud2=new stuentity ();
Stud2.setage (10);
Stud2.setname ("BDC");
Stuentity stud3=new stuentity ();
Stud3.setage (5);
Stud3.setname ("BDD");
Stuentity stud4=new stuentity ();
Stud4.setage (30);
Stud4.setname ("Aad");

List.add (STUD1);
List.add (STUD2);
List.add (STUD3);
List.add (STUD4);

Collections.sort (list, new Comparatortest ());

for (stuentity stud:list) {
System.out.println (Stud.getage () + ":" +stud.getname ());
}
}
/**
*
*/
@Override
public int Compare (stuentity stud1, stuentity stud2) {
Sort by name
int Maxname=stud1.getname (). CompareTo (Stud2.getname ());
if (maxname!=0)
return maxname;
Sort by age
int Maxage=stud1.getage ()-stud2.getage ();
if (maxage!=0)
return maxage;
}
}

Output:

Copy Code code as follows:

30:aad
10:abc
10:bdc
5:bdd

Java comparators are useful to implement the compare () callback method of the comparator interface to make a collation, and then call Collections.sort (list, new Comparatortest ()), and you can sort the list. Very convenient

Pay attention to the order of return in the Compare () method in use, and prioritize the collation to be written in the front

Entity classes:

Copy Code code as follows:

/**
* Student Entity class
*
*/
public class Stuentity {
private int studentid;//Number
private String name;
private int age;
Private String sex;//Sex
private int roomnumber;//Room number
Private String degree;//Degree
private int grade;//Grade
Private String devicenumber;//Device number
private int groupnumber;//belongs to the group
private int javascore;//Java results
private int netscore;//NET Score
Public String Getdegree () {
return degree;
}

public void Setdegree (String degree) {
This.degree = degree;
}

public int Getgrade () {
return grade;
}

public void Setgrade (int grade) {
This.grade = grade;
}

/**
* Initialization of a parameter constructor
*
* @param ID
* @param name
* @param age
* @param sex
* @param roomnumber
* @param devicenumber
* @param groupnumber
* @param javascore
* @param netscore
*/
Public stuentity (string name, int age, string sex, int roomnumber,
String devicenumber, int groupnumber, int javascore, int netscore) {
THIS.name = name;
This.age = age;
This.sex = sex;
This.roomnumber = Roomnumber;
This.devicenumber = DeviceNumber;
This.groupnumber = Groupnumber;
This.javascore = Javascore;
This.netscore = Netscore;
}

/**
* Non-parametric constructor
*/
Public stuentity () {

}

public int Getjavascore () {
return javascore;
}

public void Setjavascore (int javascore) {
This.javascore = Javascore;
}

public int Getnetscore () {
return netscore;
}

public void Setnetscore (int netscore) {
This.netscore = Netscore;
}

public int Getstudentid () {
return studentid;
}

public void Setstudentid (int studentid) {
This.studentid = StudentID;
}

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 String Getsex () {
return sex;
}

public void Setsex (String sex) {
This.sex = sex;
}

public int Getroomnumber () {
return roomnumber;
}

public void Setroomnumber (int roomnumber) {
This.roomnumber = Roomnumber;
}

Public String Getdevicenumber () {
return devicenumber;
}

public void Setdevicenumber (String devicenumber) {
This.devicenumber = DeviceNumber;
}

public int Getgroupnumber () {
return groupnumber;
}

public void Setgroupnumber (int groupnumber) {
This.groupnumber = Groupnumber;
}
}

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.