< prev index next >

src/java.base/share/classes/java/util/LinkedHashMap.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


 519      * which removes the corresponding mapping from the map, via the
 520      * {@code Iterator.remove}, {@code Set.remove},
 521      * {@code removeAll}, {@code retainAll}, and {@code clear}
 522      * operations.  It does not support the {@code add} or {@code addAll}
 523      * operations.
 524      * Its {@link Spliterator} typically provides faster sequential
 525      * performance but much poorer parallel performance than that of
 526      * {@code HashMap}.
 527      *
 528      * @return a set view of the keys contained in this map
 529      */
 530     public Set<K> keySet() {
 531         Set<K> ks = keySet;
 532         if (ks == null) {
 533             ks = new LinkedKeySet();
 534             keySet = ks;
 535         }
 536         return ks;
 537     }
 538 
 539     @Override
 540     final <T> T[] keysToArray(T[] a) {
 541         Object[] r = a;
 542         int idx = 0;
 543         for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
 544             r[idx++] = e.key;
 545         }
 546         return a;
 547     }
 548 
 549     @Override
 550     final <T> T[] valuesToArray(T[] a) {
 551         Object[] r = a;
 552         int idx = 0;
 553         for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
 554             r[idx++] = e.value;
 555         }
 556         return a;
 557     }
 558 
 559     final class LinkedKeySet extends AbstractSet<K> {
 560         public final int size()                 { return size; }
 561         public final void clear()               { LinkedHashMap.this.clear(); }
 562         public final Iterator<K> iterator() {
 563             return new LinkedKeyIterator();
 564         }
 565         public final boolean contains(Object o) { return containsKey(o); }
 566         public final boolean remove(Object key) {
 567             return removeNode(hash(key), key, null, false, true) != null;
 568         }
 569         public final Spliterator<K> spliterator()  {
 570             return Spliterators.spliterator(this, Spliterator.SIZED |
 571                                             Spliterator.ORDERED |
 572                                             Spliterator.DISTINCT);
 573         }
 574 
 575         public Object[] toArray() {
 576             return keysToArray(new Object[size]);
 577         }
 578 
 579         public <T> T[] toArray(T[] a) {
 580             return keysToArray(prepareArray(a));
 581         }
 582 
 583         public final void forEach(Consumer<? super K> action) {
 584             if (action == null)
 585                 throw new NullPointerException();
 586             int mc = modCount;
 587             for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
 588                 action.accept(e.key);
 589             if (modCount != mc)
 590                 throw new ConcurrentModificationException();
 591         }
 592     }
 593 
 594     /**
 595      * Returns a {@link Collection} view of the values contained in this map.
 596      * The collection is backed by the map, so changes to the map are
 597      * reflected in the collection, and vice-versa.  If the map is
 598      * modified while an iteration over the collection is in progress
 599      * (except through the iterator's own {@code remove} operation),
 600      * the results of the iteration are undefined.  The collection
 601      * supports element removal, which removes the corresponding
 602      * mapping from the map, via the {@code Iterator.remove},


 612     public Collection<V> values() {
 613         Collection<V> vs = values;
 614         if (vs == null) {
 615             vs = new LinkedValues();
 616             values = vs;
 617         }
 618         return vs;
 619     }
 620 
 621     final class LinkedValues extends AbstractCollection<V> {
 622         public final int size()                 { return size; }
 623         public final void clear()               { LinkedHashMap.this.clear(); }
 624         public final Iterator<V> iterator() {
 625             return new LinkedValueIterator();
 626         }
 627         public final boolean contains(Object o) { return containsValue(o); }
 628         public final Spliterator<V> spliterator() {
 629             return Spliterators.spliterator(this, Spliterator.SIZED |
 630                                             Spliterator.ORDERED);
 631         }
 632 
 633         public Object[] toArray() {
 634             return valuesToArray(new Object[size]);
 635         }
 636 
 637         public <T> T[] toArray(T[] a) {
 638             return valuesToArray(prepareArray(a));
 639         }
 640 
 641         public final void forEach(Consumer<? super V> action) {
 642             if (action == null)
 643                 throw new NullPointerException();
 644             int mc = modCount;
 645             for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
 646                 action.accept(e.value);
 647             if (modCount != mc)
 648                 throw new ConcurrentModificationException();
 649         }
 650     }
 651 
 652     /**
 653      * Returns a {@link Set} view of the mappings contained in this map.
 654      * The set is backed by the map, so changes to the map are
 655      * reflected in the set, and vice-versa.  If the map is modified
 656      * while an iteration over the set is in progress (except through
 657      * the iterator's own {@code remove} operation, or through the
 658      * {@code setValue} operation on a map entry returned by the
 659      * iterator) the results of the iteration are undefined.  The set
 660      * supports element removal, which removes the corresponding


   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


 519      * which removes the corresponding mapping from the map, via the
 520      * {@code Iterator.remove}, {@code Set.remove},
 521      * {@code removeAll}, {@code retainAll}, and {@code clear}
 522      * operations.  It does not support the {@code add} or {@code addAll}
 523      * operations.
 524      * Its {@link Spliterator} typically provides faster sequential
 525      * performance but much poorer parallel performance than that of
 526      * {@code HashMap}.
 527      *
 528      * @return a set view of the keys contained in this map
 529      */
 530     public Set<K> keySet() {
 531         Set<K> ks = keySet;
 532         if (ks == null) {
 533             ks = new LinkedKeySet();
 534             keySet = ks;
 535         }
 536         return ks;
 537     }
 538 




















 539     final class LinkedKeySet extends AbstractSet<K> {
 540         public final int size()                 { return size; }
 541         public final void clear()               { LinkedHashMap.this.clear(); }
 542         public final Iterator<K> iterator() {
 543             return new LinkedKeyIterator();
 544         }
 545         public final boolean contains(Object o) { return containsKey(o); }
 546         public final boolean remove(Object key) {
 547             return removeNode(hash(key), key, null, false, true) != null;
 548         }
 549         public final Spliterator<K> spliterator()  {
 550             return Spliterators.spliterator(this, Spliterator.SIZED |
 551                                             Spliterator.ORDERED |
 552                                             Spliterator.DISTINCT);
 553         }









 554         public final void forEach(Consumer<? super K> action) {
 555             if (action == null)
 556                 throw new NullPointerException();
 557             int mc = modCount;
 558             for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
 559                 action.accept(e.key);
 560             if (modCount != mc)
 561                 throw new ConcurrentModificationException();
 562         }
 563     }
 564 
 565     /**
 566      * Returns a {@link Collection} view of the values contained in this map.
 567      * The collection is backed by the map, so changes to the map are
 568      * reflected in the collection, and vice-versa.  If the map is
 569      * modified while an iteration over the collection is in progress
 570      * (except through the iterator's own {@code remove} operation),
 571      * the results of the iteration are undefined.  The collection
 572      * supports element removal, which removes the corresponding
 573      * mapping from the map, via the {@code Iterator.remove},


 583     public Collection<V> values() {
 584         Collection<V> vs = values;
 585         if (vs == null) {
 586             vs = new LinkedValues();
 587             values = vs;
 588         }
 589         return vs;
 590     }
 591 
 592     final class LinkedValues extends AbstractCollection<V> {
 593         public final int size()                 { return size; }
 594         public final void clear()               { LinkedHashMap.this.clear(); }
 595         public final Iterator<V> iterator() {
 596             return new LinkedValueIterator();
 597         }
 598         public final boolean contains(Object o) { return containsValue(o); }
 599         public final Spliterator<V> spliterator() {
 600             return Spliterators.spliterator(this, Spliterator.SIZED |
 601                                             Spliterator.ORDERED);
 602         }









 603         public final void forEach(Consumer<? super V> action) {
 604             if (action == null)
 605                 throw new NullPointerException();
 606             int mc = modCount;
 607             for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
 608                 action.accept(e.value);
 609             if (modCount != mc)
 610                 throw new ConcurrentModificationException();
 611         }
 612     }
 613 
 614     /**
 615      * Returns a {@link Set} view of the mappings contained in this map.
 616      * The set is backed by the map, so changes to the map are
 617      * reflected in the set, and vice-versa.  If the map is modified
 618      * while an iteration over the set is in progress (except through
 619      * the iterator's own {@code remove} operation, or through the
 620      * {@code setValue} operation on a map entry returned by the
 621      * iterator) the results of the iteration are undefined.  The set
 622      * supports element removal, which removes the corresponding


< prev index next >