src/share/classes/java/util/Hashtable.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 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


 505     }
 506 
 507     /**
 508      * Clears this hashtable so that it contains no keys.
 509      */
 510     public synchronized void clear() {
 511         Entry tab[] = table;
 512         modCount++;
 513         for (int index = tab.length; --index >= 0; )
 514             tab[index] = null;
 515         count = 0;
 516     }
 517 
 518     /**
 519      * Creates a shallow copy of this hashtable. All the structure of the
 520      * hashtable itself is copied, but the keys and values are not cloned.
 521      * This is a relatively expensive operation.
 522      *
 523      * @return  a clone of the hashtable
 524      */
 525     public synchronized Object clone() {

 526         try {
 527             Hashtable<K,V> t = (Hashtable<K,V>) super.clone();
 528             t.table = new Entry[table.length];
 529             for (int i = table.length ; i-- > 0 ; ) {
 530                 t.table[i] = (table[i] != null)
 531                     ? (Entry<K,V>) table[i].clone() : null;
 532             }
 533             t.keySet = null;
 534             t.entrySet = null;
 535             t.values = null;
 536             t.modCount = 0;
 537             return t;
 538         } catch (CloneNotSupportedException e) {
 539             // this shouldn't happen, since we are Cloneable
 540             throw new InternalError(e);
 541         }
 542     }
 543 
 544     /**
 545      * Returns a string representation of this <tt>Hashtable</tt> object


   1 /*
   2  * Copyright (c) 1994, 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


 505     }
 506 
 507     /**
 508      * Clears this hashtable so that it contains no keys.
 509      */
 510     public synchronized void clear() {
 511         Entry tab[] = table;
 512         modCount++;
 513         for (int index = tab.length; --index >= 0; )
 514             tab[index] = null;
 515         count = 0;
 516     }
 517 
 518     /**
 519      * Creates a shallow copy of this hashtable. All the structure of the
 520      * hashtable itself is copied, but the keys and values are not cloned.
 521      * This is a relatively expensive operation.
 522      *
 523      * @return  a clone of the hashtable
 524      */
 525     @Override
 526     public synchronized Hashtable<K,V> clone() {
 527         try {
 528             Hashtable<K,V> t = (Hashtable<K,V>) super.clone();
 529             t.table = new Entry[table.length];
 530             for (int i = table.length ; i-- > 0 ; ) {
 531                 t.table[i] = (table[i] != null)
 532                     ? (Entry<K,V>) table[i].clone() : null;
 533             }
 534             t.keySet = null;
 535             t.entrySet = null;
 536             t.values = null;
 537             t.modCount = 0;
 538             return t;
 539         } catch (CloneNotSupportedException e) {
 540             // this shouldn't happen, since we are Cloneable
 541             throw new InternalError(e);
 542         }
 543     }
 544 
 545     /**
 546      * Returns a string representation of this <tt>Hashtable</tt> object