最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于Java中Comparable 和 Comparator的用法

 更新時間:2023年04月06日 11:21:53   作者:CrazyDragon_King  
這篇文章主要介紹了關(guān)于Java中Comparable 和 Comparator的用法,Comparable 和 Comparator 是關(guān)于排序的兩個接口,用來實現(xiàn) Java 集合中的的排序功能,需要的朋友可以參考下

Comparable 和 Comparator

Comparable 和 Comparator 是Java的兩個和排序相關(guān)的接口,又被稱為 自然排序和定制排序。最近看了相關(guān)的內(nèi)容,現(xiàn)在來記錄以下自己的學(xué)習(xí)情況。
Comparable 和 Comparator 是關(guān)于排序的兩個接口,用來實現(xiàn) Java 集合中的的排序功能。具體作用可以查看 API 獲取。

Comparable

這是API 文檔中的簡要介紹:

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class’s natural ordering, and the class’s compareTo method is referred to as its natural comparison method. Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

用法:

需要排序?qū)嶓w類的實現(xiàn) Comparable 接口,并重寫 compareTo() 方法,就可以具有排序功能。某些會自動對元素進行排序的集合(如 TreeSet),當(dāng)把元素放入集合中,就會自動調(diào)用 CompareTo() 方法進行排序(前提是元素必須實現(xiàn)這個接口)。但是其他的地方也可以使用的,不只是局限于 TreeSet,使用還是很廣泛的。

Comparator

這是API 文檔中的簡要介紹:

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.

用法:

Comparator 是一個第三方接口,具體用法是:設(shè)計一個比較器,創(chuàng)建一個類,實現(xiàn)這個接口,重寫 compare() 方法。并且由于 Comparator 是一個函數(shù)式接口,可以使用 Lambda 表達(dá)式代替 Comparator 對象,使得代碼更加簡潔明了。

Talk is cheap, show me the code.

注意:博客中的內(nèi)容可能不會很詳細(xì),所以想看的具體細(xì)節(jié)的,應(yīng)該以書本和官方文檔為主,這里的內(nèi)容更多的是簡單的介紹一下基本的用法。

測試實體類:Dog

public class Dog implements Comparable<Dog>{
	private String name;
	private int age;
	
	public Dog(String name, int age) {
		this.name = name;
		this.age = 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;
	}
	
	@Override
	public String toString() {
		return "Dog [name=" + name + ", age=" + age + "]";
	}

	@Override
	public int compareTo(Dog dog) {
		return this.age > dog.age ? 1 : this.age < dog.age ? -1 : 0;
	}
}

測試實體類:Cat

public class Cat implements Comparable<Cat>{
	private String name;
	private Integer age;
	
	public Cat(String name, Integer age) {
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + "]";
	}

	@Override
	public int compareTo(Cat o) {
		//可以直接調(diào)用,這樣更簡單
		//調(diào)換 o.age 和 this.age 就是相反的順序
		return o.age.compareTo(this.age); 
	}
}

測試類:Test

public class Test {
	public static void main(String[] args) {
		List<Dog> dogs = new LinkedList<>();
		List<Cat> cats = new LinkedList<>();
		
		dogs.add(new Dog("大黃",6));
		dogs.add(new Dog("大白",1));
		dogs.add(new Dog("小黑",5));
		dogs.add(new Dog("旺財",3));
		dogs.add(new Dog("二哈",2));
		
		cats.add(new Cat("牛牛",3));
		cats.add(new Cat("花咪",4));
		cats.add(new Cat("咪咪",10));
		cats.add(new Cat("小黃",2));
		cats.add(new Cat("大橘",6));
		
		//參數(shù)為 null 使用 自然排序,否則使用 定制排序
		//也可以看出來 定制排序 優(yōu)先級高于 自然排序
		System.out.println("---------自然排序 升序--------");
		dogs.sort(null);   
		dogs.forEach(System.out::println);
		System.out.println("---------自然排序 降序--------");
		cats.sort(null);
		cats.forEach(System.out::println);
		
		//定制排序
	    //Comparator<Dog> c = (e1,e2)->e2.getAge() - e1.getAge();
		//dogs.sort(c) 這個就是下面這個的具體形式,
		//可以看出來參數(shù)是一個 Comparator 對象  
		System.out.println("---------定制排序 降序--------");
		dogs.sort((e1,e2)->e2.getAge() - e1.getAge());
		//流式API的簡單的應(yīng)用,效果和上面的類似,或者直接使用 forEacn 循環(huán)遍歷
		dogs.stream().forEach(System.out::println);
		System.out.println("---------定制排序 升序--------");
	//	另一種遍歷方式,可以看出來函數(shù)式編程非常靈活,我也是初學(xué),覺得很神奇。
		cats.stream()
		.sorted((e1,e2)->e1.getAge()-e2.getAge())
		.forEach(System.out::println);
	}
}

運行截圖:

運行截圖

補充說明:list.sort()方法
API 文檔中的描述:

Sorts this list according to the order induced by the specified Comparator.
All elements in this list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list). If the specified comparator is null then all elements in this list must implement the Comparable interface and the elements’s natural ordering should be used. This list must be modifiable, but need not be resizable.

可以看到,這個方法進行排序通過一個 Comparator 對象,如果傳入?yún)?shù)為 null 的話,則會進行自然排序,但是注意:自然排序的前提是相應(yīng)的實體類實現(xiàn)了 Comparable 接口,并重寫了 compareTo() 方法。

總結(jié)

簡單了解了一下 Comparable 和 Comparator 接口的作用和簡單的使用方法,這里使用了一點 Lambda 表達(dá)式的知識,不過都只是很淺顯的知識,應(yīng)該不難理解(我也只是正在學(xué)習(xí)中,哈哈)。通過一個 Dog 類和 Cat 類進行講解,我覺得很好,首先它很容易理解,并且可以快速掌握簡單的使用方法(學(xué)習(xí)是漸進式的,要一直努力學(xué)習(xí)知識才行)。

到此這篇關(guān)于關(guān)于Java中Comparable 和 Comparator的用法的文章就介紹到這了,更多相關(guān)Comparable和Comparator的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot 跨域問題的解決方案

    SpringBoot 跨域問題的解決方案

    這篇文章主要介紹了SpringBoot 跨域問題的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型方法步驟

    ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型方法步驟

    Elasticsearch存儲數(shù)據(jù)之前需要先創(chuàng)建索引,類似于結(jié)構(gòu)型數(shù)據(jù)庫建庫建表,創(chuàng)建索引時定義了每個字段的索引方式和數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型的方法步驟,需要的朋友可以參考下
    2023-09-09
  • SpringBoot實現(xiàn)elasticsearch 查詢操作(RestHighLevelClient 的案例實戰(zhàn))

    SpringBoot實現(xiàn)elasticsearch 查詢操作(RestHighLevelClient 

    這篇文章主要給大家介紹了SpringBoot如何實現(xiàn)elasticsearch 查詢操作,文中有詳細(xì)的代碼示例和操作流程,具有一定的參考價值,需要的朋友可以參考下
    2023-07-07
  • SpringMVC接收與響應(yīng)json數(shù)據(jù)的幾種方式

    SpringMVC接收與響應(yīng)json數(shù)據(jù)的幾種方式

    這篇文章主要給大家介紹了關(guān)于SpringMVC接收與響應(yīng)json數(shù)據(jù)的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用springmvc具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java正則表達(dá)式如何匹配特定html標(biāo)簽內(nèi)的內(nèi)容

    Java正則表達(dá)式如何匹配特定html標(biāo)簽內(nèi)的內(nèi)容

    這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式如何匹配特定html標(biāo)簽內(nèi)的內(nèi)容的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringBoot數(shù)據(jù)庫初始化datasource配置方式

    SpringBoot數(shù)據(jù)庫初始化datasource配置方式

    這篇文章主要為大家介紹了SpringBoot數(shù)據(jù)庫初始化datasource配置方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 詳細(xì)總結(jié)Java中常用的原子類

    詳細(xì)總結(jié)Java中常用的原子類

    今天給大家總結(jié)了一下Java常用的原子類,文中有非常詳細(xì)的介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • JavaSwing實現(xiàn)小型學(xué)生管理系統(tǒng)

    JavaSwing實現(xiàn)小型學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了JavaSwing實現(xiàn)小型學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • mybatis中${}和#{}取值的區(qū)別分析

    mybatis中${}和#{}取值的區(qū)別分析

    mybatis中使用sqlMap進行sql查詢時,經(jīng)常需要動態(tài)傳遞參數(shù),在動態(tài)SQL解析階段, #{ } 和 ${ } 會有不同的表現(xiàn),這篇文章主要給大家介紹了關(guān)于mybatis中${}和#{}取值區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • Java中Executor和Executors的區(qū)別小結(jié)

    Java中Executor和Executors的區(qū)別小結(jié)

    在Java并發(fā)編程中,Executor是一個核心接口,提供了任務(wù)執(zhí)行的抽象方法,而Executors是一個工具類,提供了創(chuàng)建各種線程池的工廠方法,Executor關(guān)注任務(wù)的執(zhí)行,而Executors關(guān)注如何創(chuàng)建適合的執(zhí)行器,感興趣的可以了解一下
    2024-10-10

最新評論

田东县| 乌兰县| 美姑县| 麟游县| 法库县| 错那县| 开江县| 和顺县| 荆州市| 三原县| 榆中县| 云龙县| 泽库县| 沐川县| 大姚县| 寻乌县| 修水县| 平陆县| 黄骅市| 万州区| 康马县| 都江堰市| 华池县| 基隆市| 黄陵县| 阳东县| 武宁县| 师宗县| 伊春市| 抚州市| 车致| 靖安县| 乐山市| 阿克苏市| 营山县| 临漳县| 科技| 沿河| 清水河县| 武安市| 广安市|