java中TreeSet集合如何实现元素的判重

  1 /*
  2 看一下部分的TreeSet源码....
  3 public class TreeSet<E> extends AbstractSet<E>
  4     implements NavigableSet<E>, Cloneable, java.io.Serializable
  5 {
  6     private transient NavigableMap<E,Object> m;
  7     //NavigableMap继承SortedMap, 二者都是接口,在TreeMap中有实现
  8     private static final Object PRESENT = new Object();
  9 
 10     TreeSet(NavigableMap<E,Object> m) {
 11         this.m = m;
 12     }
 13     ////第一种构造方法
 14     public TreeSet(Comparator<? super E> comparator) {
 15         this(new TreeMap<>(comparator));
 16     }
 17     ////第二种构造方法
 18     public TreeSet() {
 19         this(new TreeMap<E,Object>());
 20     }
 21     ..........
 22     public boolean add(E e) {
 23         return m.put(e, PRESENT)==null;
 24 
 25     /*
 26         再看一下 TreeMap 中是如何实现的 put()函数的
 27         public V put(K key, V value) {
 28         Entry<K,V> t = root;
 29         if (t == null) {
 30             compare(key, key); // type (and possibly null) check
 31 
 32             root = new Entry<>(key, value, null);
 33             size = 1;
 34             modCount++;
 35             return null;
 36         }
 37         int cmp;
 38         Entry<K,V> parent;
 39         // split comparator and comparable paths
 40         Comparator<? super K> cpr = comparator;
 41         if (cpr != null) {
 42             do {
 43                 parent = t;
 44                 cmp = cpr.compare(key, t.key);
 45                 if (cmp < 0)
 46                     t = t.left;
 47                 else if (cmp > 0)
 48                     t = t.right;
 49                 else
 50                     return t.setValue(value);
 51             } while (t != null);
 52         }
 53         else {
 54             if (key == null)
 55                 throw new NullPointerException();
 56             Comparable<? super K> k = (Comparable<? super K>) key;
 57             do {
 58                 parent = t;
 59                 cmp = k.compareTo(t.key);
 60                 if (cmp < 0)
 61                     t = t.left;
 62                 else if (cmp > 0)
 63                     t = t.right;
 64                 else
 65                     return t.setValue(value);
 66             } while (t != null);
 67         }
 68         Entry<K,V> e = new Entry<>(key, value, parent);
 69         if (cmp < 0)
 70             parent.left = e;
 71         else
 72             parent.right = e;
 73         fixAfterInsertion(e);
 74         size++;
 75         modCount++;
 76         return null;
 77     }
 78     */
 79     }
 80 }
 81 
 82 也就是说TreeSet内部实现使用TreeMap这个类来完成的
 83 TreeSet的内部实现元素之间是否相等?
 84 如果指定了Comparator(也就是利用第一种构造方法), 那么就用其中的compare方法进行比较
 85 否则就用Comparable中的compareTo()方法进行元素的比较
 86 */
 87 
 88 import java.util.*;
 89 public class CompTest{
 90     public static void main(String args[]){
 91         Set<myClass> st = new TreeSet<myClass>();
 92         st.add(new myClass(1, "fd"));
 93         st.add(new myClass(2, "fff"));
 94         st.add(new myClass(2, "tttt"));
 95         st.add(new myClass(1, "fd"));
 96 
 97         for(Iterator<myClass> it = st.iterator(); it.hasNext();)
 98             System.out.println(it.next());
 99     }
100 }
101 
102 class myClass implements Comparable<myClass>{
103 
104    public int x;
105    public String name;
106    public myClass(int x, String name){
107        this.x=x;
108        this.name=name;
109    }
110    public int compareTo(myClass tmp){
111        if(this.x==tmp.x)
112           return this.name.compareTo(tmp.name);
113        else return this.x-tmp.x;
114    }
115   
116    public String toString(){
117       return x+" "+name;
118    }
119 }

 

java中TreeSet集合如何实现元素的判重,布布扣,bubuko.com

java中TreeSet集合如何实现元素的判重

上一篇:JS调用asp.net后台方法:PageMethods


下一篇:十一:Java之GUI图形Awt和Swing