Java LinkedHashSet集合的底層原理和TreeSet集合
Collection集合體系:

LinkedHashSet:有序、不重復(fù)、無索引。
看一下之前的代碼:
package cn.chang.d4_collection_set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
/**
* 目標(biāo):整體了解下Set集合的特點(diǎn)
*/
public class SetTest1 {
public static void main(String[] args) {
// 1. 創(chuàng)建一個(gè)Set集合的對(duì)象
// 無序、不重復(fù)、無索引
//Set<Integer> set = new HashSet<Integer>(); // 多態(tài) HashSet是一個(gè)實(shí)現(xiàn)類 經(jīng)典代碼
Set<Integer> set = new LinkedHashSet<>(); // 有序 不重復(fù) 無索引
//Set<Integer> set = new TreeSet<>(); // 排序升序 不重復(fù) 無索引
set.add(666);
set.add(555);
set.add(555); // 只顯示一個(gè)555
set.add(888);
set.add(777);
System.out.println(set); // 不支持索引操作數(shù)據(jù) // 無序一次
}
}運(yùn)行結(jié)果:

可見:LinkedHashSet是無序、不重復(fù)和無索引的。
LinkedHashSet底層原理:
依然是基于哈希表(數(shù)組、鏈表和紅黑樹)實(shí)現(xiàn)的。
但是,它的每個(gè)元素都額外的多了一個(gè)雙鏈表的機(jī)制記錄它前后元素的位置。

通過雙鏈表去找數(shù)據(jù),所以就是有序的。
缺點(diǎn):每個(gè)節(jié)點(diǎn)更占內(nèi)存。
TreeSet:
特點(diǎn):不重復(fù)、無索引、可排序(默認(rèn)升序排序、按照元素的大小、由小到大排序)。
底層是基于紅黑樹實(shí)現(xiàn)的排序。
package cn.chang.d4_collection_set;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/**
* 目標(biāo): 掌握TreeSet集合的使用
*/
public class SetTest4 {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
set.add(6);
set.add(5);
set.add(5);
set.add(7);
System.out.println(set);
}
}運(yùn)行結(jié)果:

左根右,中序遍歷。
注意:
對(duì)于數(shù)值類型:Integer、Double,默認(rèn)是按照數(shù)值本身的大小進(jìn)行升序 排序。
對(duì)于字符串類型,默認(rèn)按照首字符的編號(hào)升序排列。
對(duì)于自定義類型,如Student對(duì)象,TreeSet默認(rèn)是無法直接排序的。
會(huì)有如下報(bào)錯(cuò)。

自定義排序規(guī)則:
TreeSet集合存儲(chǔ)自定義類型的對(duì)象時(shí),必須指定排序規(guī)則,支持如下兩種方式來指定比較規(guī)則:
方式一:
讓自定義類(如學(xué)生類)實(shí)現(xiàn)Comparable接口,重寫里面的compareTo方法來指定比較規(guī)則。
方式二:
通過調(diào)用TreeSet集合的有參構(gòu)造器,可以設(shè)置Comparator對(duì)象(比較器對(duì)象,用于指定比較規(guī)則)。

方式一:


代碼:
package cn.chang.d4_collection_set;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/**
* 目標(biāo): 掌握TreeSet集合的使用
*/
public class SetTest4 {
public static void main(String[] args) {
Set<Student> students = new TreeSet<>();
students.add(new Student("蜘蛛精", 23, 169.7));
students.add(new Student("紫霞", 22, 169.8));
students.add(new Student("至尊寶", 26, 165.5));
students.add(new Student("牛魔王", 22, 183.5));
System.out.println(students);
}
}
我們可以看到牛魔王沒有了。如果大小規(guī)則的相等,就重復(fù)了,牛魔王就不存了。
方式二:
package cn.chang.d4_collection_set;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/**
* 目標(biāo): 掌握TreeSet集合的使用
*/
public class SetTest4 {
public static void main(String[] args) {
Set<Student> students = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 需要按照身高升序排序
return Double.compare(s1.getHeight(), s2.getHeight());
}
});
students.add(new Student("蜘蛛精", 23, 169.7));
students.add(new Student("紫霞", 22, 169.8));
students.add(new Student("至尊寶", 26, 165.5));
students.add(new Student("牛魔王", 22, 183.5));
System.out.println(students);
}
}運(yùn)行結(jié)果:

現(xiàn)在我們有兩臺(tái)比較方案,優(yōu)先按就近原則采用方法二。
代碼的簡(jiǎn)化:
package cn.chang.d4_collection_set;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/**
* 目標(biāo): 掌握TreeSet集合的使用
*/
public class SetTest4 {
public static void main(String[] args) {
// Set<Student> students = new TreeSet<>(new Comparator<Student>() {
// @Override
// public int compare(Student s1, Student s2) {
// // 需要按照身高升序排序
// return Double.compare(s1.getHeight(), s2.getHeight());
// }
// });
Set<Student> students = new TreeSet<>((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()));
students.add(new Student("蜘蛛精", 23, 169.7));
students.add(new Student("紫霞", 22, 169.8));
students.add(new Student("至尊寶", 26, 165.5));
students.add(new Student("牛魔王", 22, 183.5));
System.out.println(students);
}
}在compare中顛倒下位置,就可以實(shí)現(xiàn)降序排列。
到此這篇關(guān)于Java LinkedHashSet集合的底層原理和TreeSet集合的文章就介紹到這了,更多相關(guān)Java LinkedHashSet和TreeSet集合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java中HashMap和HashSet的高效使用技巧分享
- Java中的Set接口實(shí)現(xiàn)類HashSet和LinkedHashSet詳解
- Java集合ArrayList、LinkedList、HashMap、HashSet最大容量
- Java中HashSet、LinkedHashSet和TreeSet區(qū)別詳解
- java的==運(yùn)算符和equals操作詳解
- Java中==和equals()的區(qū)別總結(jié)
- java兩個(gè)integer數(shù)據(jù)判斷相等用==還是equals
- 詳解Java中==和equals()的區(qū)別
- 淺談java字符串比較到底應(yīng)該用==還是equals
- java中的HashSet與 == 和 equals的區(qū)別示例解析
相關(guān)文章
SpringBoot實(shí)戰(zhàn)記錄之?dāng)?shù)據(jù)訪問
對(duì)于數(shù)據(jù)訪問層,無論是SQL還是NOSQL,Spring Boot默認(rèn)采用整合Spring Data的方式進(jìn)行統(tǒng)一處理,添加大量自動(dòng)配置,屏蔽了很多設(shè)置,下面這篇文章主要介紹了SpringBoot實(shí)戰(zhàn)記錄之?dāng)?shù)據(jù)訪問,需要的朋友可以參考下2022-04-04
微信公眾號(hào)模板消息接口開發(fā)Java實(shí)現(xiàn)方法代碼
這篇文章主要介紹了微信公眾號(hào)模板消息接口開發(fā)Java實(shí)現(xiàn)的相關(guān)資料,,該接口可以用于向關(guān)注公眾號(hào)的用戶推送消息,包括群發(fā)和指定用戶發(fā)送消息,文章詳細(xì)介紹了如何獲取公眾號(hào)的測(cè)試信息、配置接口信息和獲取access_token,需要的朋友可以參考下2024-12-12
Java Character類對(duì)單個(gè)字符操作原理解析
這篇文章主要介紹了Java Character類對(duì)單個(gè)字符操作原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題
這篇文章主要介紹了解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
spring boot+jwt實(shí)現(xiàn)api的token認(rèn)證詳解
這篇文章主要給大家介紹了關(guān)于spring boot+jwt實(shí)現(xiàn)api的token認(rèn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一學(xué)習(xí)學(xué)習(xí)吧2018-12-12
SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實(shí)踐
h2是內(nèi)存數(shù)據(jù)庫,查詢高效,可以在開發(fā)初期使用它。本文主要介紹了SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2021-09-09

