1 7 8 package java.util; 9 10 73 74 public class TreeSet<E> 75 extends AbstractSet <E> 76 implements SortedSet <E>, Cloneable , java.io.Serializable 77 { 78 private transient SortedMap <E,Object > m; private transient Set <E> keySet; 81 private static final Object PRESENT = new Object (); 83 84 87 private TreeSet(SortedMap <E,Object > m) { 88 this.m = m; 89 keySet = m.keySet(); 90 } 91 92 105 public TreeSet() { 106 this(new TreeMap <E,Object >()); 107 } 108 109 122 public TreeSet(Comparator <? super E> c) { 123 this(new TreeMap <E,Object >(c)); 124 } 125 126 141 public TreeSet(Collection <? extends E> c) { 142 this(); 143 addAll(c); 144 } 145 146 153 public TreeSet(SortedSet <E> s) { 154 this(s.comparator()); 155 addAll(s); 156 } 157 158 164 public Iterator <E> iterator() { 165 return keySet.iterator(); 166 } 167 168 173 public int size() { 174 return m.size(); 175 } 176 177 182 public boolean isEmpty() { 183 return m.isEmpty(); 184 } 185 186 195 public boolean contains(Object o) { 196 return m.containsKey(o); 197 } 198 199 209 public boolean add(E o) { 210 return m.put(o, PRESENT)==null; 211 } 212 213 222 public boolean remove(Object o) { 223 return m.remove(o)==PRESENT; 224 } 225 226 229 public void clear() { 230 m.clear(); 231 } 232 233 243 public boolean addAll(Collection <? extends E> c) { 244 if (m.size()==0 && c.size() > 0 && 246 c instanceof SortedSet && 248 m instanceof TreeMap ) { 249 SortedSet <Map.Entry <E, Object >> set = (SortedSet <Map.Entry <E, Object >>) (SortedSet ) c; 250 TreeMap <E,Object > map = (TreeMap <E, Object >) m; 251 Comparator <? super E> cc = (Comparator <E>) set.comparator(); 252 Comparator <? super E> mc = map.comparator(); 253 if (cc==mc || (cc != null && cc.equals(mc))) { 254 map.addAllForTreeSet(set, PRESENT); 255 return true; 256 } 257 } 258 return super.addAll(c); 259 } 260 261 309 public SortedSet <E> subSet(E fromElement, E toElement) { 310 return new TreeSet <E>(m.subMap(fromElement, toElement)); 311 } 312 313 346 public SortedSet <E> headSet(E toElement) { 347 return new TreeSet <E>(m.headMap(toElement)); 348 } 349 350 385 public SortedSet <E> tailSet(E fromElement) { 386 return new TreeSet <E>(m.tailMap(fromElement)); 387 } 388 389 396 public Comparator <? super E> comparator() { 397 return m.comparator(); 398 } 399 400 406 public E first() { 407 return m.firstKey(); 408 } 409 410 416 public E last() { 417 return m.lastKey(); 418 } 419 420 426 public Object clone() { 427 TreeSet <E> clone = null; 428 try { 429 clone = (TreeSet <E>) super.clone(); 430 } catch (CloneNotSupportedException e) { 431 throw new InternalError (); 432 } 433 434 clone.m = new TreeMap <E,Object >(m); 435 clone.keySet = clone.m.keySet(); 436 437 return clone; 438 } 439 440 452 private void writeObject(java.io.ObjectOutputStream s) 453 throws java.io.IOException { 454 s.defaultWriteObject(); 456 457 s.writeObject(m.comparator()); 459 460 s.writeInt(m.size()); 462 463 for (Iterator i=m.keySet().iterator(); i.hasNext(); ) 465 s.writeObject(i.next()); 466 } 467 468 472 private void readObject(java.io.ObjectInputStream s) 473 throws java.io.IOException , ClassNotFoundException { 474 s.defaultReadObject(); 476 477 Comparator <E> c = (Comparator <E>) s.readObject(); 479 480 TreeMap <E,Object > tm; 482 if (c==null) 483 tm = new TreeMap <E,Object >(); 484 else 485 tm = new TreeMap <E,Object >(c); 486 m = tm; 487 keySet = m.keySet(); 488 489 int size = s.readInt(); 491 492 tm.readTreeSet(size, s, PRESENT); 493 } 494 495 private static final long serialVersionUID = -2479143000061671589L; 496 } 497 | Popular Tags |