src/java.base/share/classes/java/util/HashMap.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 53710 49adf961fcb1
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   136  * @since   1.2
   136  * @since   1.2
   137  */
   137  */
   138 public class HashMap<K,V> extends AbstractMap<K,V>
   138 public class HashMap<K,V> extends AbstractMap<K,V>
   139     implements Map<K,V>, Cloneable, Serializable {
   139     implements Map<K,V>, Cloneable, Serializable {
   140 
   140 
       
   141     @java.io.Serial
   141     private static final long serialVersionUID = 362498820763181265L;
   142     private static final long serialVersionUID = 362498820763181265L;
   142 
   143 
   143     /*
   144     /*
   144      * Implementation notes.
   145      * Implementation notes.
   145      *
   146      *
   909             keySet = ks;
   910             keySet = ks;
   910         }
   911         }
   911         return ks;
   912         return ks;
   912     }
   913     }
   913 
   914 
       
   915     /**
       
   916      * Prepares the array for {@link Collection#toArray(Object[])} implementation.
       
   917      * If supplied array is smaller than this map size, a new array is allocated.
       
   918      * If supplied array is bigger than this map size, a null is written at size index.
       
   919      *
       
   920      * @param a an original array passed to {@code toArray()} method
       
   921      * @param <T> type of array elements
       
   922      * @return an array ready to be filled and returned from {@code toArray()} method.
       
   923      */
       
   924     @SuppressWarnings("unchecked")
       
   925     final <T> T[] prepareArray(T[] a) {
       
   926         int size = this.size;
       
   927         if (a.length < size) {
       
   928             return (T[]) java.lang.reflect.Array
       
   929                     .newInstance(a.getClass().getComponentType(), size);
       
   930         }
       
   931         if (a.length > size) {
       
   932             a[size] = null;
       
   933         }
       
   934         return a;
       
   935     }
       
   936 
       
   937     /**
       
   938      * Fills an array with this map keys and returns it. This method assumes
       
   939      * that input array is big enough to fit all the keys. Use
       
   940      * {@link #prepareArray(Object[])} to ensure this.
       
   941      *
       
   942      * @param a an array to fill
       
   943      * @param <T> type of array elements
       
   944      * @return supplied array
       
   945      */
       
   946     <T> T[] keysToArray(T[] a) {
       
   947         Object[] r = a;
       
   948         Node<K,V>[] tab;
       
   949         int idx = 0;
       
   950         if (size > 0 && (tab = table) != null) {
       
   951             for (Node<K,V> e : tab) {
       
   952                 for (; e != null; e = e.next) {
       
   953                     r[idx++] = e.key;
       
   954                 }
       
   955             }
       
   956         }
       
   957         return a;
       
   958     }
       
   959 
       
   960     /**
       
   961      * Fills an array with this map values and returns it. This method assumes
       
   962      * that input array is big enough to fit all the values. Use
       
   963      * {@link #prepareArray(Object[])} to ensure this.
       
   964      *
       
   965      * @param a an array to fill
       
   966      * @param <T> type of array elements
       
   967      * @return supplied array
       
   968      */
       
   969     <T> T[] valuesToArray(T[] a) {
       
   970         Object[] r = a;
       
   971         Node<K,V>[] tab;
       
   972         int idx = 0;
       
   973         if (size > 0 && (tab = table) != null) {
       
   974             for (Node<K,V> e : tab) {
       
   975                 for (; e != null; e = e.next) {
       
   976                     r[idx++] = e.value;
       
   977                 }
       
   978             }
       
   979         }
       
   980         return a;
       
   981     }
       
   982 
   914     final class KeySet extends AbstractSet<K> {
   983     final class KeySet extends AbstractSet<K> {
   915         public final int size()                 { return size; }
   984         public final int size()                 { return size; }
   916         public final void clear()               { HashMap.this.clear(); }
   985         public final void clear()               { HashMap.this.clear(); }
   917         public final Iterator<K> iterator()     { return new KeyIterator(); }
   986         public final Iterator<K> iterator()     { return new KeyIterator(); }
   918         public final boolean contains(Object o) { return containsKey(o); }
   987         public final boolean contains(Object o) { return containsKey(o); }
   920             return removeNode(hash(key), key, null, false, true) != null;
   989             return removeNode(hash(key), key, null, false, true) != null;
   921         }
   990         }
   922         public final Spliterator<K> spliterator() {
   991         public final Spliterator<K> spliterator() {
   923             return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
   992             return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
   924         }
   993         }
       
   994 
       
   995         public Object[] toArray() {
       
   996             return keysToArray(new Object[size]);
       
   997         }
       
   998 
       
   999         public <T> T[] toArray(T[] a) {
       
  1000             return keysToArray(prepareArray(a));
       
  1001         }
       
  1002 
   925         public final void forEach(Consumer<? super K> action) {
  1003         public final void forEach(Consumer<? super K> action) {
   926             Node<K,V>[] tab;
  1004             Node<K,V>[] tab;
   927             if (action == null)
  1005             if (action == null)
   928                 throw new NullPointerException();
  1006                 throw new NullPointerException();
   929             if (size > 0 && (tab = table) != null) {
  1007             if (size > 0 && (tab = table) != null) {
   968         public final Iterator<V> iterator()     { return new ValueIterator(); }
  1046         public final Iterator<V> iterator()     { return new ValueIterator(); }
   969         public final boolean contains(Object o) { return containsValue(o); }
  1047         public final boolean contains(Object o) { return containsValue(o); }
   970         public final Spliterator<V> spliterator() {
  1048         public final Spliterator<V> spliterator() {
   971             return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0);
  1049             return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0);
   972         }
  1050         }
       
  1051 
       
  1052         public Object[] toArray() {
       
  1053             return valuesToArray(new Object[size]);
       
  1054         }
       
  1055 
       
  1056         public <T> T[] toArray(T[] a) {
       
  1057             return valuesToArray(prepareArray(a));
       
  1058         }
       
  1059 
   973         public final void forEach(Consumer<? super V> action) {
  1060         public final void forEach(Consumer<? super V> action) {
   974             Node<K,V>[] tab;
  1061             Node<K,V>[] tab;
   975             if (action == null)
  1062             if (action == null)
   976                 throw new NullPointerException();
  1063                 throw new NullPointerException();
   977             if (size > 0 && (tab = table) != null) {
  1064             if (size > 0 && (tab = table) != null) {
  1401      *             <i>size</i> (an int, the number of key-value
  1488      *             <i>size</i> (an int, the number of key-value
  1402      *             mappings), followed by the key (Object) and value (Object)
  1489      *             mappings), followed by the key (Object) and value (Object)
  1403      *             for each key-value mapping.  The key-value mappings are
  1490      *             for each key-value mapping.  The key-value mappings are
  1404      *             emitted in no particular order.
  1491      *             emitted in no particular order.
  1405      */
  1492      */
       
  1493     @java.io.Serial
  1406     private void writeObject(java.io.ObjectOutputStream s)
  1494     private void writeObject(java.io.ObjectOutputStream s)
  1407         throws IOException {
  1495         throws IOException {
  1408         int buckets = capacity();
  1496         int buckets = capacity();
  1409         // Write out the threshold, loadfactor, and any hidden stuff
  1497         // Write out the threshold, loadfactor, and any hidden stuff
  1410         s.defaultWriteObject();
  1498         s.defaultWriteObject();
  1418      * @param s the stream
  1506      * @param s the stream
  1419      * @throws ClassNotFoundException if the class of a serialized object
  1507      * @throws ClassNotFoundException if the class of a serialized object
  1420      *         could not be found
  1508      *         could not be found
  1421      * @throws IOException if an I/O error occurs
  1509      * @throws IOException if an I/O error occurs
  1422      */
  1510      */
       
  1511     @java.io.Serial
  1423     private void readObject(java.io.ObjectInputStream s)
  1512     private void readObject(java.io.ObjectInputStream s)
  1424         throws IOException, ClassNotFoundException {
  1513         throws IOException, ClassNotFoundException {
  1425         // Read in the threshold (ignored), loadfactor, and any hidden stuff
  1514         // Read in the threshold (ignored), loadfactor, and any hidden stuff
  1426         s.defaultReadObject();
  1515         s.defaultReadObject();
  1427         reinitialize();
  1516         reinitialize();