淺談TreeSet中的兩種排序方式
直接上代碼:
package exercise1;
public class Person implements Comparable{
private int id;
private String name;
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public int compareTo(Object o) {
if(o instanceof Person){
Person p=(Person)o;
return this.name.compareTo(p.name);
}
return 0;
}
}
package exercise1;
//TreeSet下的自然排序和定制排序
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import org.junit.Test;
//定制排序
public class DisOrder {
@Test
public void unNature(){
Comparator com=new Comparator() {
public int compare(Object o1, Object o2) {
if(o1 instanceof Person && o2 instanceof Person){
Person p1=(Person)o1;
Person p2=(Person)o2;
return p1.getName().compareTo(p2.getName());
}
return 0;
}
};
Set set=new TreeSet(com);
set.add(new Person(111,"MM"));
set.add(new Person(222,"DD"));
set.add(new Person(333,"GG"));
for(Object obj:set){
System.out.println(obj);
}
}
//自然排序
@Test
public void nature() {
Set set=new TreeSet();
set.add("aa");
set.add("bb");
set.add("ff");
set.add("zz");
for(Object obj:set){
System.out.println(obj);
}
}
}
以上這篇淺談TreeSet中的兩種排序方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中的FactoryBean與ObjectFactory詳解
這篇文章主要介紹了Spring中的FactoryBean與ObjectFactory詳解,FactoryBean是一種特殊的bean,本身又是個(gè)工廠,實(shí)現(xiàn)了FactoryBean的bean會(huì)被注冊(cè)到容器中,需要的朋友可以參考下2023-12-12
Spring-Boot 集成Solr客戶端的詳細(xì)步驟
本篇文章主要介紹了Spring-Boot 集成Solr客戶端的詳細(xì)步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
SpringBoot深入講解單元測(cè)試與熱部署應(yīng)用
這篇文章介紹了SpringBoot單元測(cè)試與熱部署,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)之設(shè)置微服務(wù)接口開發(fā)模塊
這篇文章主要為大家介紹了實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)之接口開發(fā)醫(yī)院設(shè)置微服務(wù)模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
spring?retry實(shí)現(xiàn)方法請(qǐng)求重試的使用步驟
這篇文章主要介紹了spring?retry實(shí)現(xiàn)方法請(qǐng)求重試及使用步驟,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
詳解Java動(dòng)態(tài)代理的實(shí)現(xiàn)機(jī)制
這篇文章主要為大家詳細(xì)介紹了Java動(dòng)態(tài)代理的實(shí)現(xiàn)機(jī)制,感興趣的小伙伴們可以參考一下2016-03-03
springboot之redis cache TTL選項(xiàng)的使用
這篇文章主要介紹了springboot之redis cache TTL選項(xiàng)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

