Java中實(shí)現(xiàn)Comparable和Comparator對(duì)象比較
當(dāng)需要排序的集合或數(shù)組不是單純的數(shù)字型時(shí),通??梢允褂肅omparator或Comparable,以簡單的方式實(shí)現(xiàn)對(duì)象排序或自定義排序。
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering. ------API
對(duì)字符串List可以直接sort進(jìn)行排序, 那是因?yàn)镾tring 這個(gè)對(duì)象已經(jīng)幫我們實(shí)現(xiàn)了 Comparable接口 , 所以我們的 Person 如果想排序, 也要實(shí)現(xiàn)一個(gè)比較器。
一. Comparator
對(duì)Linkedlist存儲(chǔ)的對(duì)象進(jìn)行排序
import java.util.Comparator;
import java.util.LinkedList;
class Person{
private float height;
private String name;
Person(float height)
{
this.height=height;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class PersonHeight implements Comparator<Person>{
@Override
//重寫compare方法,return<0不變,return>0則交換順序(保持升序)
public int compare(Person e1, Person e2) {
if(e1.getHeight() < e2.getHeight()){
return 1;
} else {
return -1;
}
}
}
public class Question3 {
public static void main(String[] args) {
Person p1=new Person(23.4f);
p1.setName("Stud1");
Person p2=new Person(2.34f);
p2.setName("Stud2");
Person p3=new Person(34.32f);
p3.setName("Stud3");
Person p4=new Person(56.45f);
p4.setName("Stud4");
Person p5=new Person(21.4f);
p5.setName("Stud5");
LinkedList<Person> al=new LinkedList<Person>();
al.add(p1);
al.add(p2);
al.add(p3);
al.add(p4);
al.add(p5);
//調(diào)用sort方法,實(shí)現(xiàn)排序
Collections.sort(al, new PersonHeight());
//遍歷輸出
for(Person p:al)
System.out.println(p.getName());
}
}
附加:
//對(duì)日期進(jìn)行排序
/**
* 如果o1小于o2,返回一個(gè)負(fù)數(shù);如果o1大于o2,返回一個(gè)正數(shù);如果他們相等,則返回0;
*/
@Override
public int compare(Step o1, Step o2) {
Date acceptTime1=UtilTool.strToDate(o1.getAcceptTime(), null);
Date acceptTime2=UtilTool.strToDate(o2.getAcceptTime(), null);
//對(duì)日期字段進(jìn)行升序,如果欲降序可采用before方法
if(acceptTime1.after(acceptTime2)) return 1;
return -1;
}
二. Comparable
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
class Person implements Comparable{
private float height;
private String name;
Person(float height)
{
this.height=height;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(this.height>((Person)o).height){
return 1;
}else
return -1;
}
}
public class Question3 {
public static void main(String[] args) {
Person p1=new Person(23.4f);
p1.setName("Stud1");
Person p2=new Person(2.34f);
p2.setName("Stud2");
Person p3=new Person(34.32f);
p3.setName("Stud3");
Person p4=new Person(56.45f);
p4.setName("Stud4");
Person p5=new Person(21.4f);
p5.setName("Stud5");
LinkedList<Person> al=new LinkedList<Person>();
al.add(p1);
al.add(p2);
al.add(p3);
al.add(p4);
al.add(p5);
Collections.sort(al);
for(Person p:al)
System.out.println(p.getName());
}
}
三.比較
Comparable 定義在 Person類的內(nèi)部。
Comparator 是定義在Person的外部的, 此時(shí)我們的Person類的結(jié)構(gòu)不需要有任何變化。
兩種方法各有優(yōu)劣, 用Comparable 簡單, 只要實(shí)現(xiàn)Comparable 接口的對(duì)象直接就成為一個(gè)可以比較的對(duì)象,但是需要修改源代碼, 用Comparator 的好處是不需要修改源代碼, 而是另外實(shí)現(xiàn)一個(gè)比較器, 當(dāng)某個(gè)自定義的對(duì)象需要作比較的時(shí)候,把比較器和對(duì)象一起傳遞過去就可以比大小了, 并且在Comparator 里面用戶可以自己實(shí)現(xiàn)復(fù)雜的可以通用的邏輯,使其可以匹配一些比較簡單的對(duì)象,那樣就可以節(jié)省很多重復(fù)勞動(dòng)了。
相關(guān)文章
java實(shí)現(xiàn)連接mysql數(shù)據(jù)庫單元測(cè)試查詢數(shù)據(jù)的實(shí)例代碼
下面小編就為大家?guī)硪黄猨ava實(shí)現(xiàn)連接mysql數(shù)據(jù)庫單元測(cè)試查詢數(shù)據(jù)的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
利用Spring MVC+Mybatis實(shí)現(xiàn)Mysql分頁數(shù)據(jù)查詢的過程詳解
這篇文章主要給大家介紹了關(guān)于利用Spring MVC+Mybatis實(shí)現(xiàn)Mysql分頁數(shù)據(jù)查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
Java使用Instant時(shí)輸出的時(shí)間比預(yù)期少了八個(gè)小時(shí)
在Java中,LocalDateTime表示沒有時(shí)區(qū)信息的日期和時(shí)間,而Instant表示基于UTC的時(shí)間點(diǎn),本文主要介紹了Java使用Instant時(shí)輸出的時(shí)間比預(yù)期少了八個(gè)小時(shí)的問題解決,感興趣的可以了解一下2024-09-09
springboot實(shí)現(xiàn)注冊(cè)加密與登錄解密功能(demo)
這篇文章主要介紹了springboot實(shí)現(xiàn)注冊(cè)的加密與登錄的解密功能,本文通過demo實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
詳解Java的Hibernate框架中的set映射集與SortedSet映射
這篇文章主要介紹了詳解Java的Hibernate框架中的set映射集與SortedSet映射,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12
SpringBoot的Admin服務(wù)監(jiān)控詳解
這篇文章主要介紹了SpringBoot的Admin服務(wù)監(jiān)控詳解,Spring Boot Admin(SBA)是一個(gè)開源的社區(qū)項(xiàng)目,用于管理和監(jiān)控 Spring Boot 應(yīng)用程序,需要的朋友可以參考下2024-01-01

