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

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

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

Comparable 和 Comparator

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

Comparable

這是API 文檔中的簡(jiǎn)要介紹:

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類(lèi)的實(shí)現(xiàn) Comparable 接口,并重寫(xiě) compareTo() 方法,就可以具有排序功能。某些會(huì)自動(dòng)對(duì)元素進(jìn)行排序的集合(如 TreeSet),當(dāng)把元素放入集合中,就會(huì)自動(dòng)調(diào)用 CompareTo() 方法進(jìn)行排序(前提是元素必須實(shí)現(xiàn)這個(gè)接口)。但是其他的地方也可以使用的,不只是局限于 TreeSet,使用還是很廣泛的。

Comparator

這是API 文檔中的簡(jiǎn)要介紹:

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 是一個(gè)第三方接口,具體用法是:設(shè)計(jì)一個(gè)比較器,創(chuàng)建一個(gè)類(lèi),實(shí)現(xiàn)這個(gè)接口,重寫(xiě) compare() 方法。并且由于 Comparator 是一個(gè)函數(shù)式接口,可以使用 Lambda 表達(dá)式代替 Comparator 對(duì)象,使得代碼更加簡(jiǎn)潔明了。

Talk is cheap, show me the code.

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

測(cè)試實(shí)體類(lè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;
	}
}

測(cè)試實(shí)體類(lèi):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)用,這樣更簡(jiǎn)單
		//調(diào)換 o.age 和 this.age 就是相反的順序
		return o.age.compareTo(this.age); 
	}
}

測(cè)試類(lèi):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("旺財(cái)",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 使用 自然排序,否則使用 定制排序
		//也可以看出來(lái) 定制排序 優(yōu)先級(jí)高于 自然排序
		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) 這個(gè)就是下面這個(gè)的具體形式,
		//可以看出來(lái)參數(shù)是一個(gè) Comparator 對(duì)象  
		System.out.println("---------定制排序 降序--------");
		dogs.sort((e1,e2)->e2.getAge() - e1.getAge());
		//流式API的簡(jiǎn)單的應(yīng)用,效果和上面的類(lèi)似,或者直接使用 forEacn 循環(huán)遍歷
		dogs.stream().forEach(System.out::println);
		System.out.println("---------定制排序 升序--------");
	//	另一種遍歷方式,可以看出來(lái)函數(shù)式編程非常靈活,我也是初學(xué),覺(jué)得很神奇。
		cats.stream()
		.sorted((e1,e2)->e1.getAge()-e2.getAge())
		.forEach(System.out::println);
	}
}

運(yùn)行截圖:

運(yùn)行截圖

補(bǔ)充說(shuō)明: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.

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

總結(jié)

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

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

相關(guān)文章

  • SpringBoot 跨域問(wèn)題的解決方案

    SpringBoot 跨域問(wèn)題的解決方案

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

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

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

    SpringBoot實(shí)現(xiàn)elasticsearch 查詢(xún)操作(RestHighLevelClient 

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

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

    這篇文章主要給大家介紹了關(guān)于SpringMVC接收與響應(yīng)json數(shù)據(jù)的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用springmvc具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(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)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringBoot數(shù)據(jù)庫(kù)初始化datasource配置方式

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

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

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

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

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

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

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

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

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

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

最新評(píng)論

沅陵县| 资源县| 广灵县| 垦利县| 垦利县| 桑日县| 临安市| 德庆县| 综艺| 丹寨县| 平果县| 碌曲县| 盘山县| 谷城县| 淳化县| 榕江县| 商南县| 喜德县| 大竹县| 九龙城区| 邵东县| 台中市| 新竹县| 武胜县| 修水县| 阜城县| 姚安县| 新郑市| 厦门市| 龙江县| 大城县| 莫力| 左权县| 礼泉县| 宁阳县| 澄迈县| 奈曼旗| 桂东县| 漠河县| 凤凰县| 张家口市|