| 1 7 8 package java.util; 9 10 80 81 public class TreeMap<K,V> 82 extends AbstractMap <K,V> 83 implements SortedMap <K,V>, Cloneable , java.io.Serializable  84 { 85 91 private Comparator <? super K> comparator = null; 92 93 private transient Entry<K,V> root = null; 94 95 98 private transient int size = 0; 99 100 103 private transient int modCount = 0; 104 105 private void incrementSize() { modCount++; size++; } 106 private void decrementSize() { modCount++; size--; } 107 108 121 public TreeMap() { 122 } 123 124 137 public TreeMap(Comparator <? super K> c) { 138 this.comparator = c; 139 } 140 141 155 public TreeMap(Map <? extends K, ? extends V> m) { 156 putAll(m); 157 } 158 159 168 public TreeMap(SortedMap <K, ? extends V> m) { 169 comparator = m.comparator(); 170 try { 171 buildFromSorted(m.size(), m.entrySet().iterator(), null, null); 172 } catch (java.io.IOException cannotHappen) { 173 } catch (ClassNotFoundException cannotHappen) { 174 } 175 } 176 177 178 180 185 public int size() { 186 return size; 187 } 188 189 203 public boolean containsKey(Object key) { 204 return getEntry(key) != null; 205 } 206 207 220 public boolean containsValue(Object value) { 221 return (root==null ? false : 222 (value==null ? valueSearchNull(root) 223 : valueSearchNonNull(root, value))); 224 } 225 226 private boolean valueSearchNull(Entry n) { 227 if (n.value == null) 228 return true; 229 230 return (n.left != null && valueSearchNull(n.left)) || 232 (n.right != null && valueSearchNull(n.right)); 233 } 234 235 private boolean valueSearchNonNull(Entry n, Object value) { 236 if (value.equals(n.value)) 238 return true; 239 240 return (n.left != null && valueSearchNonNull(n.left, value)) || 242 (n.right != null && valueSearchNonNull(n.right, value)); 243 } 244 245 264 public V get(Object key) { 265 Entry<K,V> p = getEntry(key); 266 return (p==null ? null : p.value); 267 } 268 269 276 public Comparator <? super K> comparator() { 277 return comparator; 278 } 279 280 286 public K firstKey() { 287 return key(firstEntry()); 288 } 289 290 296 public K lastKey() { 297 return key(lastEntry()); 298 } 299 300 313 public void putAll(Map <? extends K, ? extends V> map) { 314 int mapSize = map.size(); 315 if (size==0 && mapSize!=0 && map instanceof SortedMap ) { 316 Comparator c = ((SortedMap )map).comparator(); 317 if (c == comparator || (c != null && c.equals(comparator))) { 318 ++modCount; 319 try { 320 buildFromSorted(mapSize, map.entrySet().iterator(), 321 null, null); 322 } catch (java.io.IOException cannotHappen) { 323 } catch (ClassNotFoundException cannotHappen) { 324 } 325 return; 326 } 327 } 328 super.putAll(map); 329 } 330 331 343 private Entry<K,V> getEntry(Object key) { 344 Entry<K,V> p = root; 345 K k = (K) key; 346 while (p != null) { 347 int cmp = compare(k, p.key); 348 if (cmp == 0) 349 return p; 350 else if (cmp < 0) 351 p = p.left; 352 else 353 p = p.right; 354 } 355 return null; 356 } 357 358 364 private Entry<K,V> getCeilEntry(K key) { 365 Entry<K,V> p = root; 366 if (p==null) 367 return null; 368 369 while (true) { 370 int cmp = compare(key, p.key); 371 if (cmp == 0) { 372 return p; 373 } else if (cmp < 0) { 374 if (p.left != null) 375 p = p.left; 376 else 377 return p; 378 } else { 379 if (p.right != null) { 380 p = p.right; 381 } else { 382 Entry<K,V> parent = p.parent; 383 Entry<K,V> ch = p; 384 while (parent != null && ch == parent.right) { 385 ch = parent; 386 parent = parent.parent; 387 } 388 return parent; 389 } 390 } 391 } 392 } 393 394 399 private Entry<K,V> getPrecedingEntry(K key) { 400 Entry<K,V> p = root; 401 if (p==null) 402 return null; 403 404 while (true) { 405 int cmp = compare(key, p.key); 406 if (cmp > 0) { 407 if (p.right != null) 408 p = p.right; 409 else 410 return p; 411 } else { 412 if (p.left != null) { 413 p = p.left; 414 } else { 415 Entry<K,V> parent = p.parent; 416 Entry<K,V> ch = p; 417 while (parent != null && ch == parent.left) { 418 ch = parent; 419 parent = parent.parent; 420 } 421 return parent; 422 } 423 } 424 } 425 } 426 427 431 private static <K> K key(Entry<K,?> e) { 432 if (e==null) 433 throw new NoSuchElementException (); 434 return e.key; 435 } 436 437 455 public V put(K key, V value) { 456 Entry<K,V> t = root; 457 458 if (t == null) { 459 incrementSize(); 460 root = new Entry<K,V>(key, value, null); 461 return null; 462 } 463 464 while (true) { 465 int cmp = compare(key, t.key); 466 if (cmp == 0) { 467 return t.setValue(value); 468 } else if (cmp < 0) { 469 if (t.left != null) { 470 t = t.left; 471 } else { 472 incrementSize(); 473 t.left = new Entry<K,V>(key, value, t); 474 fixAfterInsertion(t.left); 475 return null; 476 } 477 } else { if (t.right != null) { 479 t = t.right; 480 } else { 481 incrementSize(); 482 t.right = new Entry<K,V>(key, value, t); 483 fixAfterInsertion(t.right); 484 return null; 485 } 486 } 487 } 488 } 489 490 505 public V remove(Object key) { 506 Entry<K,V> p = getEntry(key); 507 if (p == null) 508 return null; 509 510 V oldValue = p.value; 511 deleteEntry(p); 512 return oldValue; 513 } 514 515 518 public void clear() { 519 modCount++; 520 size = 0; 521 root = null; 522 } 523 524 530 public Object clone() { 531 TreeMap <K,V> clone = null; 532 try { 533 clone = (TreeMap <K,V>) super.clone(); 534 } catch (CloneNotSupportedException e) { 535 throw new InternalError (); 536 } 537 538 clone.root = null; 540 clone.size = 0; 541 clone.modCount = 0; 542 clone.entrySet = null; 543 544 try { 546 clone.buildFromSorted(size, entrySet().iterator(), null, null); 547 } catch (java.io.IOException cannotHappen) { 548 } catch (ClassNotFoundException cannotHappen) { 549 } 550 551 return clone; 552 } 553 554 555 557 562 private transient volatile Set <Map.Entry <K,V>> entrySet = null; 563 564 576 public Set <K> keySet() { 577 if (keySet == null) { 578 keySet = new AbstractSet <K>() { 579 public Iterator <K> iterator() { 580 return new KeyIterator(); 581 } 582 583 public int size() { 584 return TreeMap.this.size(); 585 } 586 587 public boolean contains(Object o) { 588 return containsKey(o); 589 } 590 591 public boolean remove(Object o) { 592 int oldSize = size; 593 TreeMap.this.remove(o); 594 return size != oldSize; 595 } 596 597 public void clear() { 598 TreeMap.this.clear(); 599 } 600 }; 601 } 602 return keySet; 603 } 604 605 618 public Collection <V> values() { 619 if (values == null) { 620 values = new AbstractCollection <V>() { 621 public Iterator <V> iterator() { 622 return new ValueIterator(); 623 } 624 625 public int size() { 626 return TreeMap.this.size(); 627 } 628 629 public boolean contains(Object o) { 630 for (Entry<K,V> e = firstEntry(); e != null; e = successor(e)) 631 if (valEquals(e.getValue(), o)) 632 return true; 633 return false; 634 } 635 636 public boolean remove(Object o) { 637 for (Entry<K,V> e = firstEntry(); e != null; e = successor(e)) { 638 if (valEquals(e.getValue(), o)) { 639 deleteEntry(e); 640 return true; 641 } 642 } 643 return false; 644 } 645 646 public void clear() { 647 TreeMap.this.clear(); 648 } 649 }; 650 } 651 return values; 652 } 653 654 668 public Set <Map.Entry <K,V>> entrySet() { 669 if (entrySet == null) { 670 entrySet = new AbstractSet <Map.Entry <K,V>>() { 671 public Iterator <Map.Entry <K,V>> iterator() { 672 return new EntryIterator(); 673 } 674 675 public boolean contains(Object o) { 676 if (!(o instanceof Map.Entry )) 677 return false; 678 Map.Entry <K,V> entry = (Map.Entry <K,V>) o; 679 V value = entry.getValue(); 680 Entry<K,V> p = getEntry(entry.getKey()); 681 return p != null && valEquals(p.getValue(), value); 682 } 683 684 public boolean remove(Object o) { 685 if (!(o instanceof Map.Entry )) 686 return false; 687 Map.Entry <K,V> entry = (Map.Entry <K,V>) o; 688 V value = entry.getValue(); 689 Entry<K,V> p = getEntry(entry.getKey()); 690 if (p != null && valEquals(p.getValue(), value)) { 691 deleteEntry(p); 692 return true; 693 } 694 return false; 695 } 696 697 public int size() { 698 return TreeMap.this.size(); 699 } 700 701 public void clear() { 702 TreeMap.this.clear(); 703 } 704 }; 705 } 706 return entrySet; 707 } 708 709 753 public SortedMap <K,V> subMap(K fromKey, K toKey) { 754 return new SubMap(fromKey, toKey); 755 } 756 757 794 public SortedMap <K,V> headMap(K toKey) { 795 return new SubMap(toKey, true); 796 } 797 798 833 public SortedMap <K,V> tailMap(K fromKey) { 834 return new SubMap(fromKey, false); 835 } 836 837 private class SubMap 838 extends AbstractMap <K,V> 839 implements SortedMap <K,V>, java.io.Serializable { 840 private static final long serialVersionUID = -6520786458950516097L; 841 842 846 private boolean fromStart = false, toEnd = false; 847 private K fromKey, toKey; 848 849 SubMap(K fromKey, K toKey) { 850 if (compare(fromKey, toKey) > 0) 851 throw new IllegalArgumentException ("fromKey > toKey"); 852 this.fromKey = fromKey; 853 this.toKey = toKey; 854 } 855 856 SubMap(K key, boolean headMap) { 857 compare(key, key); 859 if (headMap) { 860 fromStart = true; 861 toKey = key; 862 } else { 863 toEnd = true; 864 fromKey = key; 865 } 866 } 867 868 SubMap(boolean fromStart, K fromKey, boolean toEnd, K toKey) { 869 this.fromStart = fromStart; 870 this.fromKey= fromKey; 871 this.toEnd = toEnd; 872 this.toKey = toKey; 873 } 874 875 public boolean isEmpty() { 876 return entrySet.isEmpty(); 877 &nbs
|