摘要:若是通過進(jìn)行排序的話當(dāng)前集合采用的。最后附上一個(gè)標(biāo)準(zhǔn)的使用的方法自然排序是實(shí)現(xiàn)接口并且重寫了方法的另一個(gè)則是通過并且重寫方法
首先簡(jiǎn)單介紹下TreeSet和TreeMap的兩種排序:
自然排序
通過comparator排序
private static void compareWithCpmparator(){ TreeSettreeSet =new TreeSet<>(); List list =new ArrayList<>(); list.add("a"); list.add("d"); list.add("b"); treeSet.addAll(list); Iterator iterator =treeSet.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } Comparator comparator1 = (Comparator ) treeSet.comparator(); if (comparator1 == null){ System.out.println("comparator1是空"); }else { System.out.println("comparator1不是空"); } } public static void main(String[] args) { compareWithCpmparator(); }
運(yùn)行之后的結(jié)果如下:
a b d comparator1是空
這段代碼里面獲取的comparator是空的,Debug一遍,發(fā)現(xiàn)這個(gè)方法其實(shí)調(diào)用的是NavigableMap里面的comparator
public Comparator super E> comparator() { return m.comparator(); }
查看官網(wǎng)上對(duì)其的介紹:
Comparator super K> comparator() Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys. Returns: the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys
在調(diào)用這個(gè)方法的時(shí)候若是自然排序,那么會(huì)返回一個(gè)null。若是通過comparator進(jìn)行排序的話當(dāng)前集合采用的comparator。
查看官網(wǎng)對(duì)reeSet的無參構(gòu)造器的解釋:
/**
* Constructs a new, empty tree set, sorted according to the * natural ordering of its elements. All elements inserted into * the set must implement the {@link Comparable} interface. * Furthermore, all such elements must be mutually * comparable: {@code e1.compareTo(e2)} must not throw a * {@code ClassCastException} for any elements {@code e1} and * {@code e2} in the set. If the user attempts to add an element * to the set that violates this constraint (for example, the user * attempts to add a string element to a set whose elements are * integers), the {@code add} call will throw a * {@code ClassCastException}.
在使用TreeSet的時(shí)候,插入的元素需要實(shí)現(xiàn)Comparable這個(gè)接口,而剛剛的元素是String,查看String的代碼發(fā)現(xiàn):
public final class String implements java.io.Serializable, Comparable, CharSequence {
確實(shí)實(shí)現(xiàn)了,再測(cè)試一個(gè)沒有實(shí)現(xiàn)的元素:
public class PojoTest { private int id; private String name; public PojoTest() { } 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 PojoTest(int id, String name) { this.id = id; this.name = name; } } private static void com(){ TreeSettreeSet =new TreeSet<>(); treeSet.add(new PojoTest(1,"a")); treeSet.add(new PojoTest(2,"b")); treeSet.add(new PojoTest(3,"c")); Iterator iterator =treeSet.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next().getName()); } }
運(yùn)行結(jié)果如下:
Exception in thread "main" java.lang.ClassCastException: SetAndMap.TreeSetAndTreeMap.PojoTest cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(TreeMap.java:1294) at java.util.TreeMap.put(TreeMap.java:538) at java.util.TreeSet.add(TreeSet.java:255) at SetAndMap.TreeSetAndTreeMap.TestTreeSet.com(TestTreeSet.java:77) at SetAndMap.TreeSetAndTreeMap.TestTreeSet.main(TestTreeSet.java:88)
很明顯,所以放在TreeSet里面的元素要么是實(shí)現(xiàn)Comparable了的自然排序,要么是通過comparator來進(jìn)行排序的。
最后附上一個(gè)標(biāo)準(zhǔn)的使用Comparator的方法:
private static void construct(){ Comparator自然排序:comparator =new Comparator () { @Override public int compare(String o1, String o2) { if(o1.toCharArray()[0] >o2.toCharArray()[0]){ return -1; }else if(o1.toCharArray()[0] == o2.toCharArray()[0]){ return 0; }else{ return 1; } } }; TreeSet treeSet =new TreeSet<>(comparator); List list =new ArrayList<>(); list.add("a"); list.add("d"); list.add("b"); treeSet.addAll(list); Iterator iterator =treeSet.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } Comparator comparator1 = (Comparator ) treeSet.comparator(); TreeSet treeSet1 =new TreeSet<>(comparator1); treeSet1.add("c"); treeSet1.add("g"); treeSet1.add("a"); Iterator iterator1 =treeSet1.iterator(); while (iterator1.hasNext()){ System.out.println(iterator1.next()); }
是實(shí)現(xiàn)Comparable接口并且重寫了compareTo方法的
另一個(gè)comparator則是通過comparator并且重寫compare方法
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/69506.html
摘要:下面總結(jié)一下集合常用的三個(gè)子類吧無序,允許為,底層是散列表紅黑樹,非線程同步有序,不允許為,底層是紅黑樹非線程同步迭代有序,允許為,底層是雙向鏈表,非線程同步從結(jié)論而言我們就可以根據(jù)自己的實(shí)際情況來使用了。 前言 聲明,本文用的是jdk1.8 前面章節(jié)回顧: Collection總覽 List集合就這么簡(jiǎn)單【源碼剖析】 Map集合、散列表、紅黑樹介紹 HashMap就是這么簡(jiǎn)單【源碼...
摘要:很多文章或書籍在介紹紅黑樹的時(shí)候直接上來就是紅黑樹的個(gè)基本性質(zhì)插入刪除操作等。這也不奇怪,算法的作者就是紅黑樹的作者之一。所以,理解樹對(duì)掌握紅黑樹是至關(guān)重要的。 本文主要包括以下內(nèi)容: 什么是2-3樹 2-3樹的插入操作 紅黑樹與2-3樹的等價(jià)關(guān)系 《算法4》和《算法導(dǎo)論》上關(guān)于紅黑樹的差異 紅黑樹的5條基本性質(zhì)的分析 紅黑樹與2-3-4樹的等價(jià)關(guān)系 紅黑樹的插入、刪除操作 JDK ...
摘要:第三階段常見對(duì)象的學(xué)習(xí)集合框架集合在實(shí)際需求中,我們常常會(huì)遇到這樣的問題,在諸多的數(shù)據(jù)中,通過其編號(hào)來尋找某一些信息,從而進(jìn)行查看或者修改,例如通過學(xué)號(hào)查詢學(xué)生信息。面試題和的區(qū)別是單列集合的頂層接口,有子接口和。 第三階段 JAVA常見對(duì)象的學(xué)習(xí) 集合框架——Map集合 showImg(https://segmentfault.com/img/remote/1460000019683...
摘要:存儲(chǔ)元素實(shí)際為存儲(chǔ)的鍵值對(duì)為的,為固定對(duì)象遍歷方式支持正向反向迭代器遍歷和遍歷順序迭代器實(shí)現(xiàn)順序遍歷實(shí)現(xiàn)逆序遍歷反向迭代器實(shí)現(xiàn) HashSet & TreeSet小結(jié) 聲明 文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處:https://segmentfault.com/u/yzwall HashSet小結(jié) HashSet簡(jiǎn)介 HashSet是一個(gè)沒有重復(fù)元素的集;HashSet可以存儲(chǔ)null...
摘要:使用默認(rèn)隨機(jī)源對(duì)指定列表進(jìn)行置換。將集合排序使用二分搜索法搜索指定列表,以獲得指定對(duì)象根據(jù)元素的自然順序,返回給定的最大元素。 1_Map集合概述和特點(diǎn) A:Map接口概述 查看API可以知道: 將鍵映射到值的對(duì)象 一個(gè)映射不能包含重復(fù)的鍵 每個(gè)鍵最多只能映射到一個(gè)值 B:Map接口和Collection接口的不同 Map是雙列的,Collection是單列的 Map...
閱讀 3270·2021-09-29 09:34
閱讀 3620·2021-09-10 10:51
閱讀 2015·2021-09-10 10:50
閱讀 7027·2021-08-12 13:31
閱讀 3061·2019-08-30 15:54
閱讀 1685·2019-08-30 15:44
閱讀 1488·2019-08-29 12:26
閱讀 2716·2019-08-26 18:36