Java TreeSet 添加失敗的解決
Java 中TreeSet 是Set的一個子類。
Set、List、Map區別
Set是一個無序、不允許重復的集合。
List(ArrayList、Vector等)是有序、可重復的。
Map(HashMap等)是鍵值對。
public static void demo() { TreeSet<Person> ts = new TreeSet<>(); ts.add(new Person('張三', 23)); ts.add(new Person('李四', 13)); ts.add(new Person('周七', 13)); ts.add(new Person('王五', 43)); ts.add(new Person('趙六', 33)); System.out.println(ts); }
此處會報異常,異常類型為java.lang.ClassCastException
這是因為Person類沒有實現Comparable 接口,并實現compareTo函數。
compareTo函數就是TreeSet用來判斷是否唯一的函數。
public class TreeEntity implements Comparable<Object> { @JsonProperty(value = 'Name') private String name = ''; /** * 名稱(字段名) * */ public final String getName() { return name; } public final void setName(String value) { name = value; } @JsonProperty(value = 'Header') private String header = ''; /** * 顯示的名稱(字段別名) */ public final String getHeader() { if (header.isEmpty()) {header = name; } return header; } public final void setHeader(String value) { header = value; } @Setter(AccessLevel.PROTECTED) @JsonProperty(value = 'Childrens') private TreeSet<TreeEntity> childrens; /** * 子節點集合 * */ public final TreeSet<TreeEntity> getChildrens() { return childrens; } public final void setChildrens(TreeSet<TreeEntity> value) { childrens = value; } @Override public int compareTo(Object o) { TreeEntity te = (TreeEntity) o; if (te == null)return 1; return this.getName().compareTo(te.getName()); } /** * 構造函數 */ public TreeEntity() { childrens = new TreeSet<TreeEntity>(); } } }}
入上圖中的TreeEntity類重寫了compareTo函數,則是通過name屬性來判斷是否唯一。
在TreeSet.add()函數中,會觸發此compareTo函數,如果判斷不唯一,不會添加進去,但是代碼不會報異常。
compareTo函數返回值說明:
為什么返回0,只會存一個元素,返回-1會倒序存儲,返回1會怎么存就怎么取呢?原因在于TreeSet底層其實是一個二叉樹機構,且每插入一個新元素(第一個除外)都會調用compareTo()方法去和上一個插入的元素作比較,并按二叉樹的結構進行排列。
1. 如果將compareTo()返回值寫死為0,元素值每次比較,都認為是相同的元素,這時就不再向TreeSet中插入除第一個外的新元素。所以TreeSet中就只存在插入的第一個元素。
2. 如果將compareTo()返回值寫死為1,元素值每次比較,都認為新插入的元素比上一個元素大,于是二叉樹存儲時,會存在根的右側,讀取時就是正序排列的。
3. 如果將compareTo()返回值寫死為-1,元素值每次比較,都認為新插入的元素比上一個元素小,于是二叉樹存儲時,會存在根的左側,讀取時就是倒序序排列的。
補充知識:compareTo方法重寫錯誤導致TreeSet中無法添加對象
問題描述:
定義了一個實現Comparable接口的類R,包含一個int變量count。在測試類中添加了一個count為-3的對象后,便無法添加count為1的對象。但是可以添加count比-3小的和count為正數的R對象。
錯誤原因:
compareTo方法重寫錯誤。
代碼:
package test20160302;import java.util.TreeSet;class R implements Comparable<Object>{ int count; public R(int count){ this.count = count; System.out.println('count:'+count); } public String toString(){ return 'R[count:'+this.count+']'; } public boolean equals(Object obj){ if(this == obj)return true; if(obj!=null && obj.getClass()==R.class){ return this.count == ((R)obj).count; }else return false; } public int compareTo(Object obj){ R r = (R)obj; System.out.println('用來比較的數:'+this.count); System.out.println('被比較的數:'+r.count); return this.count<r.count?-1:this.count>1?1:0; }}public class TreeSetTest3 { public static void main(String[] args) { TreeSet ts = new TreeSet(); ts.add(new R(-3)); ts.add(new R(-1)); System.out.println(ts); }}
輸出:
count:-3
用來比較的數:-3
被比較的數:-3
count:-1
用來比較的數:-1
被比較的數:-3
[R[count:-3]]
測試:
- 只添加-3,9,1無法添加
- 只添加9,除0外均可以添加。
- 添加-2,9后,1無法添加
- 添加-1,9后,1無法添加
- 添加-1,2后,1無法添加
- 添加-3后,-1無法添加
- 添加-1后,-3無法添加
以上這篇Java TreeSet 添加失敗的解決就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
