Two ways of realizing the TreeSet comparator and Comparable__java

Source: Internet
Author: User
Tags comparable object object sorted by name

TreeSet

Conditions

Same type

Design to sort

Comparable is natural (you can modify the case of a class)

Comparator is custom (in the case of a class cannot be modified) higher priority

Instructions for use:

Generally used is the natural sort, but when the class can not be modified to have to use the comparator method, the following demo using two ways.

Code Overall Description:

The Birthday property of the employee class is the custom class mydate type

Employee by comparable way by default is sorted by name

Then the birth date is sorted using a customized comparator method

Package TestContainer;
	public class MyDate {private int day;
	private int month;

	private int year;
	public int getday () {return day;
	public void Setday (int day) {this.day = day;
	public int getmonth () {return month;
	The public void setmonth (int month) {this.month = month;
	public int getyear () {return year;
		@Override public int hashcode () {final int prime = 31;
		int result = 1;
		result = Prime * result + day;
		result = Prime * result + month;
		result = Prime * result + year;
	return result;
		@Override public boolean equals (Object obj) {if (this = obj) return true;
		if (obj = null) return false;
		if (GetClass ()!= Obj.getclass ()) return false;
		MyDate other = (mydate) obj;
		if (day!= other.day) return false;
		if (month!= other.month) return false;
		if (year!= other.year) return false;
	return true;
	public void setyear (int year) {this.year = year; MyDate (int day, int month, int) {super ();
		This.day = day;
		This.month = month;
	This.year = year;
	Public MyDate () {super ();
	@Override public String toString () {return "mydate [day= + day +", month= "+ month +", year= "+ year +]";
 }

}

Package TestContainer;
Whether to override the CompareTo () and Equals () methods, although the comparison is a string or integer CompareTo () method, it is also necessary to determine whether multiple employee is equal if you put the employee in the TreeSet
	public class Employee implements comparable{private String name;
		@Override public int hashcode () {final int prime = 31;
		int result = 1;
		result = Prime * result + age;
		result = Prime * result + ((birthday = null)? 0:birthday.hashcode ());
		result = Prime * result + ((name = = null) 0:name.hashcode ());
	return result;
		@Override public boolean equals (Object obj) {if (this = obj) return true;
		if (obj = null) return false;
		if (GetClass ()!= Obj.getclass ()) return false;
		Employee other = (employee) obj;
		if (age!= other.age) return false;
		if (birthday = = null) {if (other.birthday!= null) return false;
		else if (!birthday.equals (Other.birthday)) return false;
		if (name = = null) {if (other.name!= null) return false;
		else if (!name.equals (Other.name)) return false;
return true;	private int age;

	Private MyDate birthday;
	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 MyDate Getbirthday () {return birthday;
	public void Setbirthday (mydate birthday) {this.birthday = birthday;
	Public Employee () {super ();
		Public Employee (String name, int age, mydate birthday) {super ();
		THIS.name = name;
		This.age = age;
	This.birthday = Birthday; @Override public String toString () {return "Employee [name=" + name +], age= "+ Age +", birthday= "+ Birthday +
	"]";
			@Override public int compareTo (Object o) {if (o instanceof employee) {Employee employee= (employee) O;
int I=this.getname (). CompareTo (Employee.getname ());
			if (i==0) {//int j=this.getage ()-employee.getage ();////if (j==0) {////////}//return J;//}
		return i;
	return 0; }
}
 

Package TestContainer;

Import static org.junit.assert.*;
Import Java.util.Comparator;

Import Java.util.TreeSet;

Import Org.junit.Test; public class Testemployee {//Natural sort, employee implements comparable interface, and press name to sort @Test public void Test () {Employee E1 = new Em
		Ployee ("Haid", New MyDate (2, 2, 1985));
		Employee E2 = new Employee ("Qiao", New MyDate (13, 10, 1991));
		Employee E3 = new Employee ("Alex", New MyDate (2, 2, 1996));
		Employee E4 = New Employee ("Tom", Panax Notoginseng, New MyDate (2, 2, 1980));

		Employee E5 = new Employee ("Alex", New MyDate (13, 10, 1989)); Custom sort Comparator Comparator Comparator = new Comparator () {public int compare (object O1, Object O2) {if (O1 in
					Stanceof Employee && O2 instanceof Employee} {Employee E1 = (employee) O1;
					Employee E2 = (employee) O2;
					int i = E1.getbirthday (). getyear ()-E2.getbirthday (). getyear ();
						if (i = = 0) {int j = e1.getbirthday (). getmonth ()-E2.getbirthday (). getmonth (); if (j = = 0){return E1.getbirthday (). Getday ()-E2.getbirthday (). Getday ();
					} return J;
				return i;

			return 0;

		}

		};
		TreeSet set = new TreeSet (comparator);
		Set.add (E1);
		Set.add (E2);
		Set.add (E3);
		Set.add (E4);

		Set.add (E5);

	For (object Object:set) System.out.println (object);
 }
}

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.