src/share/classes/java/util/TreeMap.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 600         deleteEntry(p);
 601         return oldValue;
 602     }
 603 
 604     /**
 605      * Removes all of the mappings from this map.
 606      * The map will be empty after this call returns.
 607      */
 608     public void clear() {
 609         modCount++;
 610         size = 0;
 611         root = null;
 612     }
 613 
 614     /**
 615      * Returns a shallow copy of this {@code TreeMap} instance. (The keys and
 616      * values themselves are not cloned.)
 617      *
 618      * @return a shallow copy of this map
 619      */
 620     public Object clone() {

 621         TreeMap<K,V> clone = null;
 622         try {
 623             clone = (TreeMap<K,V>) super.clone();
 624         } catch (CloneNotSupportedException e) {
 625             throw new InternalError(e);
 626         }
 627 
 628         // Put clone into "virgin" state (except for comparator)
 629         clone.root = null;
 630         clone.size = 0;
 631         clone.modCount = 0;
 632         clone.entrySet = null;
 633         clone.navigableKeySet = null;
 634         clone.descendingMap = null;
 635 
 636         // Initialize clone with our mappings
 637         try {
 638             clone.buildFromSorted(size, entrySet().iterator(), null, null);
 639         } catch (java.io.IOException cannotHappen) {
 640         } catch (ClassNotFoundException cannotHappen) {


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 600         deleteEntry(p);
 601         return oldValue;
 602     }
 603 
 604     /**
 605      * Removes all of the mappings from this map.
 606      * The map will be empty after this call returns.
 607      */
 608     public void clear() {
 609         modCount++;
 610         size = 0;
 611         root = null;
 612     }
 613 
 614     /**
 615      * Returns a shallow copy of this {@code TreeMap} instance. (The keys and
 616      * values themselves are not cloned.)
 617      *
 618      * @return a shallow copy of this map
 619      */
 620     @Override
 621     public TreeMap<K,V> clone() {
 622         TreeMap<K,V> clone = null;
 623         try {
 624             clone = (TreeMap<K,V>) super.clone();
 625         } catch (CloneNotSupportedException e) {
 626             throw new InternalError(e);
 627         }
 628 
 629         // Put clone into "virgin" state (except for comparator)
 630         clone.root = null;
 631         clone.size = 0;
 632         clone.modCount = 0;
 633         clone.entrySet = null;
 634         clone.navigableKeySet = null;
 635         clone.descendingMap = null;
 636 
 637         // Initialize clone with our mappings
 638         try {
 639             clone.buildFromSorted(size, entrySet().iterator(), null, null);
 640         } catch (java.io.IOException cannotHappen) {
 641         } catch (ClassNotFoundException cannotHappen) {