Comparable internal comparator and comparator external comparator for Java

Source: Internet
Author: User
Tags comparable java comparator

Here is a brief introduction to Java's comparable internal comparator and comparator external comparator usage implementation

So let's do a sort of between objects, first build a model or call Javaben. As follows:

The use of the 1.Java comparable internal comparator is implemented:

Comparable internal comparator (to have the entity class Javaben (testcomparable) implement the comparable interface and override the CompareTo (Object O) method)

The code is implemented as follows:

public interface comparable<t> This interface forces the overall ordering of objects for each class that implements it.

This sort is called the natural ordering of the class, and the compareTo method of the class is called its natural comparison method .

Package com.xiawei.test;

public class Testcomparable implements comparable<object> {

/**
* Bill Number
*/
private int sequence = 0;
/**
* Billing Name
*/
Private String Billname = "";

Public testcomparable (int sequence,string billname) {
This.sequence = sequence;
This.billname = Billname;
}

public int getsequence () {
return sequence;
}
public void setsequence (int sequence) {
This.sequence = sequence;
}
Public String Getbillname () {
return billname;
}
public void Setbillname (String billname) {
This.billname = Billname;
}

Overriding the CompareTo () method of the comparable interface

int compareTo(T o) compares the order of this object with the specified object. If the object is less than, equal to, or greater than the specified object, it returns a negative integer, 0, or a positive integer, respectively.

//Parameters: o -the object to compare.

//returns: a negative integer, 0, or a positive integer, depending on whether the object is less than, equal to, or greater than the specified object.


@Override
public int compareTo (Object obj) {
TODO auto-generated Method Stub
Return This.getsequence ()-((testcomparable) obj). Getsequence ();
}

@Override
Public String toString () {
Return "testcomparable [Bill number =" + sequence + ", Bill name =" + Billname + "]";
}

}

=======================================================================================

To test the effect, here is the sort by comparing the Bill object's billing number to the private int sequence:

Package Com.xiawei;

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

Import com.xiawei.test.TestComparable;

public class Main {

public static void Main (string[] args) {
Comparable internal comparator (to have the entity class (Model) implement the comparable interface and override the CompareTo (Object O) method)
list<testcomparable> lis = new arraylist<testcomparable> ();
Lis.add (New testcomparable (1, "cost of living expenses"));
Lis.add (New testcomparable (2, "travel expenses"));
Collections.sort (LIS);
System.out.println (LIS);

}

}

Output Result:

[testcomparable [Bill number = 1, Bill name = Cost of living],

testcomparable [Bill number = 2, Bill name = travel expenses]]

=======================================================================================

Also here we do a sort of between objects, so first build a model or call Javaben. As follows:

The use of the 2.Java comparator external comparator is implemented:

Comparator external Comparator (define a class to implement the comparator interface and override the Compare (Object o1,object O2) method)

Package Com.xiawei.model;

public class Billinfo {

/**
* Bill Number
*/
private int sequence = 0;
/**
* Billing Name
*/
Private String Billname = "";
/**
* Bill Amount
*/
Private double Billmoney = 0;
/**
* Transfers Date
*/
Private String date = "";

Public billinfo (int sequence,string billname,double billmoney,string date) {
This.sequence = sequence;
This.billname = Billname;
This.billmoney = Billmoney;
This.date = date;
}

public int getsequence () {
return sequence;
}
public void setsequence (int sequence) {
This.sequence = sequence;
}
Public String Getbillname () {
return billname;
}
public void Setbillname (String billname) {
This.billname = Billname;
}
Public double Getbillmoney () {
return Billmoney;
}
public void Setbillmoney (double Billmoney) {
This.billmoney = Billmoney;
}
Public String getDate () {
return date;
}
public void SetDate (String date) {
This.date = date;
}


@Override
Public String toString () {
Return "Billinfo [Bill number =" + sequence + ", Bill name =" + Billname + ", Bill amount =" + Billmoney + ", Transfers date ="
+ Date + "]";
}
}

=====================================================================================

Define a class to implement the comparator interface separately and override the Compare (Object o1,object O2) method with the following code:

int Compare(T o1,t O2): Compares the two parameters used to sort. Returns a negative integer, 0, or a positive integer, respectively, based on the first parameter less than, equal to, or greater than the second parameter.

//Parameters: o1 -the first object to compare. o2-the second object to compare.

//return: a negative integer, 0, or a positive integer is returned according to the first parameter less than, equal to, or greater than the second parameter.

Package com.xiawei.summary;

Import Java.util.Comparator;

Import Com.xiawei.model.BillInfo;

public class Billinfocomparator implements Comparator<BillInfo> {

@Override
public int Compare (Billinfo B1, Billinfo B2) {
Billinfo bill1 = (billinfo) B1;
Billinfo bill2 = (billinfo) B2;
int result = Bill1.getsequence () > Bill2.getsequence ()? 1:
(bill1.getsequence () = = Bill2.getsequence ())? 0:-1;

return result = result = = 0? (Bill1.getbillmoney () > Bill2.getbillmoney () 1:-1): result;
}


}

=========================================================================

To test the effect, this is also compared by the Bill object's bill number private int sequence to compare the sort:

Package Com.xiawei;

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

Import Com.xiawei.model.BillInfo;
Import Com.xiawei.summary.BillInfoComparator;

public class Main {

public static void Main (string[] args) {


Comparator external comparator (define a class to implement the comparator interface and override the Compare (Object o1,object O2) method)
list<billinfo> list = new arraylist<billinfo> ();
List.add (New Billinfo (4, "food expenditure", 100.0, "2017-01-01"));
List.add (New Billinfo (2, "Tourism expenditure", 200.0, "2017-02-01"));
List.add (New Billinfo (5, "education expenditure", 300.0, "2017-03-01"));
List.add (New Billinfo (3, "investment expenditure", 400.0, "2017-04-01"));
List.add (New Billinfo (1, "Traffic expenses", 500.0, "2017-05-01"));
Collections.sort (List,new billinfocomparator ());
SYSTEM.OUT.PRINTLN (list);
}

}

Output Result:

[Billinfo [Bill number = 1, Bill name = traffic expense, Bill amount = 500.0, transfers date = 2017-05-01],

Billinfo [Bill number = 2, Bill name = travel expense, Bill amount = 200.0, transfers date = 2017-02-01],

Billinfo [Bill number = 3, Bill name = investment expense, Bill amount = 400.0, transfers date = 2017-04-01],

Billinfo [Bill number = 4, Bill name = food expense, Bill amount = 100.0, transfers date = 2017-01-01],

Billinfo [Bill number = 5, Bill name = Education expense, Bill amount = 300.0, transfers date = 2017-03-01]]

Comparable internal comparator and comparator external comparator for Java

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.