< prev index next >

src/java.base/share/classes/java/util/HashMap.java

Print this page


   1 /*
   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.
   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


 894      * while an iteration over the set is in progress (except through
 895      * the iterator's own {@code remove} operation), the results of
 896      * the iteration are undefined.  The set supports element removal,
 897      * which removes the corresponding mapping from the map, via the
 898      * {@code Iterator.remove}, {@code Set.remove},
 899      * {@code removeAll}, {@code retainAll}, and {@code clear}
 900      * operations.  It does not support the {@code add} or {@code addAll}
 901      * operations.
 902      *
 903      * @return a set view of the keys contained in this map
 904      */
 905     public Set<K> keySet() {
 906         Set<K> ks = keySet;
 907         if (ks == null) {
 908             ks = new KeySet();
 909             keySet = ks;
 910         }
 911         return ks;
 912     }
 913 
 914     /**
 915      * Prepares the array for {@link Collection#toArray(Object[])} implementation.
 916      * If supplied array is smaller than this map size, a new array is allocated.
 917      * If supplied array is bigger than this map size, a null is written at size index.
 918      *
 919      * @param a an original array passed to {@code toArray()} method
 920      * @param <T> type of array elements
 921      * @return an array ready to be filled and returned from {@code toArray()} method.
 922      */
 923     @SuppressWarnings("unchecked")
 924     final <T> T[] prepareArray(T[] a) {
 925         int size = this.size;
 926         if (a.length < size) {
 927             return (T[]) java.lang.reflect.Array
 928                     .newInstance(a.getClass().getComponentType(), size);
 929         }
 930         if (a.length > size) {
 931             a[size] = null;
 932         }
 933         return a;
 934     }
 935 
 936     /**
 937      * Fills an array with this map keys and returns it. This method assumes
 938      * that input array is big enough to fit all the keys. Use
 939      * {@link #prepareArray(Object[])} to ensure this.
 940      *
 941      * @param a an array to fill
 942      * @param <T> type of array elements
 943      * @return supplied array
 944      */
 945     <T> T[] keysToArray(T[] a) {
 946         Object[] r = a;
 947         Node<K,V>[] tab;
 948         int idx = 0;
 949         if (size > 0 && (tab = table) != null) {
 950             for (Node<K,V> e : tab) {
 951                 for (; e != null; e = e.next) {
 952                     r[idx++] = e.key;
 953                 }
 954             }
 955         }
 956         return a;
 957     }
 958 
 959     /**
 960      * Fills an array with this map values and returns it. This method assumes
 961      * that input array is big enough to fit all the values. Use
 962      * {@link #prepareArray(Object[])} to ensure this.
 963      *
 964      * @param a an array to fill
 965      * @param <T> type of array elements
 966      * @return supplied array
 967      */
 968     <T> T[] valuesToArray(T[] a) {
 969         Object[] r = a;
 970         Node<K,V>[] tab;
 971         int idx = 0;
 972         if (size > 0 && (tab = table) != null) {
 973             for (Node<K,V> e : tab) {
 974                 for (; e != null; e = e.next) {
 975                     r[idx++] = e.value;
 976                 }
 977             }
 978         }
 979         return a;
 980     }
 981 
 982     final class KeySet extends AbstractSet<K> {
 983         public final int size()                 { return size; }
 984         public final void clear()               { HashMap.this.clear(); }
 985         public final Iterator<K> iterator()     { return new KeyIterator(); }
 986         public final boolean contains(Object o) { return containsKey(o); }
 987         public final boolean remove(Object key) {
 988             return removeNode(hash(key), key, null, false, true) != null;
 989         }
 990         public final Spliterator<K> spliterator() {
 991             return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
 992         }
 993 
 994         public Object[] toArray() {
 995             return keysToArray(new Object[size]);
 996         }
 997 
 998         public <T> T[] toArray(T[] a) {
 999             return keysToArray(prepareArray(a));
1000         }
1001 
1002         public final void forEach(Consumer<? super K> action) {
1003             Node<K,V>[] tab;
1004             if (action == null)
1005                 throw new NullPointerException();
1006             if (size > 0 && (tab = table) != null) {
1007                 int mc = modCount;
1008                 for (Node<K,V> e : tab) {
1009                     for (; e != null; e = e.next)
1010                         action.accept(e.key);
1011                 }
1012                 if (modCount != mc)
1013                     throw new ConcurrentModificationException();
1014             }
1015         }
1016     }
1017 
1018     /**
1019      * Returns a {@link Collection} view of the values contained in this map.
1020      * The collection is backed by the map, so changes to the map are
1021      * reflected in the collection, and vice-versa.  If the map is


1030      *
1031      * @return a view of the values contained in this map
1032      */
1033     public Collection<V> values() {
1034         Collection<V> vs = values;
1035         if (vs == null) {
1036             vs = new Values();
1037             values = vs;
1038         }
1039         return vs;
1040     }
1041 
1042     final class Values extends AbstractCollection<V> {
1043         public final int size()                 { return size; }
1044         public final void clear()               { HashMap.this.clear(); }
1045         public final Iterator<V> iterator()     { return new ValueIterator(); }
1046         public final boolean contains(Object o) { return containsValue(o); }
1047         public final Spliterator<V> spliterator() {
1048             return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0);
1049         }
1050 
1051         public Object[] toArray() {
1052             return valuesToArray(new Object[size]);
1053         }
1054 
1055         public <T> T[] toArray(T[] a) {
1056             return valuesToArray(prepareArray(a));
1057         }
1058 
1059         public final void forEach(Consumer<? super V> action) {
1060             Node<K,V>[] tab;
1061             if (action == null)
1062                 throw new NullPointerException();
1063             if (size > 0 && (tab = table) != null) {
1064                 int mc = modCount;
1065                 for (Node<K,V> e : tab) {
1066                     for (; e != null; e = e.next)
1067                         action.accept(e.value);
1068                 }
1069                 if (modCount != mc)
1070                     throw new ConcurrentModificationException();
1071             }
1072         }
1073     }
1074 
1075     /**
1076      * Returns a {@link Set} view of the mappings contained in this map.
1077      * The set is backed by the map, so changes to the map are
1078      * reflected in the set, and vice-versa.  If the map is modified


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


 894      * while an iteration over the set is in progress (except through
 895      * the iterator's own {@code remove} operation), the results of
 896      * the iteration are undefined.  The set supports element removal,
 897      * which removes the corresponding mapping from the map, via the
 898      * {@code Iterator.remove}, {@code Set.remove},
 899      * {@code removeAll}, {@code retainAll}, and {@code clear}
 900      * operations.  It does not support the {@code add} or {@code addAll}
 901      * operations.
 902      *
 903      * @return a set view of the keys contained in this map
 904      */
 905     public Set<K> keySet() {
 906         Set<K> ks = keySet;
 907         if (ks == null) {
 908             ks = new KeySet();
 909             keySet = ks;
 910         }
 911         return ks;
 912     }
 913 




































































 914     final class KeySet extends AbstractSet<K> {
 915         public final int size()                 { return size; }
 916         public final void clear()               { HashMap.this.clear(); }
 917         public final Iterator<K> iterator()     { return new KeyIterator(); }
 918         public final boolean contains(Object o) { return containsKey(o); }
 919         public final boolean remove(Object key) {
 920             return removeNode(hash(key), key, null, false, true) != null;
 921         }
 922         public final Spliterator<K> spliterator() {
 923             return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
 924         }









 925         public final void forEach(Consumer<? super K> action) {
 926             Node<K,V>[] tab;
 927             if (action == null)
 928                 throw new NullPointerException();
 929             if (size > 0 && (tab = table) != null) {
 930                 int mc = modCount;
 931                 for (Node<K,V> e : tab) {
 932                     for (; e != null; e = e.next)
 933                         action.accept(e.key);
 934                 }
 935                 if (modCount != mc)
 936                     throw new ConcurrentModificationException();
 937             }
 938         }
 939     }
 940 
 941     /**
 942      * Returns a {@link Collection} view of the values contained in this map.
 943      * The collection is backed by the map, so changes to the map are
 944      * reflected in the collection, and vice-versa.  If the map is


 953      *
 954      * @return a view of the values contained in this map
 955      */
 956     public Collection<V> values() {
 957         Collection<V> vs = values;
 958         if (vs == null) {
 959             vs = new Values();
 960             values = vs;
 961         }
 962         return vs;
 963     }
 964 
 965     final class Values extends AbstractCollection<V> {
 966         public final int size()                 { return size; }
 967         public final void clear()               { HashMap.this.clear(); }
 968         public final Iterator<V> iterator()     { return new ValueIterator(); }
 969         public final boolean contains(Object o) { return containsValue(o); }
 970         public final Spliterator<V> spliterator() {
 971             return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0);
 972         }









 973         public final void forEach(Consumer<? super V> action) {
 974             Node<K,V>[] tab;
 975             if (action == null)
 976                 throw new NullPointerException();
 977             if (size > 0 && (tab = table) != null) {
 978                 int mc = modCount;
 979                 for (Node<K,V> e : tab) {
 980                     for (; e != null; e = e.next)
 981                         action.accept(e.value);
 982                 }
 983                 if (modCount != mc)
 984                     throw new ConcurrentModificationException();
 985             }
 986         }
 987     }
 988 
 989     /**
 990      * Returns a {@link Set} view of the mappings contained in this map.
 991      * The set is backed by the map, so changes to the map are
 992      * reflected in the set, and vice-versa.  If the map is modified


< prev index next >