< prev index next >

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

Print this page


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


4385         @Override
4386         public void forEach(Consumer<? super E> action) {
4387             Objects.requireNonNull(action);
4388             for (E e : a) {
4389                 action.accept(e);
4390             }
4391         }
4392 
4393         @Override
4394         public void replaceAll(UnaryOperator<E> operator) {
4395             Objects.requireNonNull(operator);
4396             E[] a = this.a;
4397             for (int i = 0; i < a.length; i++) {
4398                 a[i] = operator.apply(a[i]);
4399             }
4400         }
4401 
4402         @Override
4403         public void sort(Comparator<? super E> c) {
4404             Arrays.sort(a, c);





























4405         }
4406     }
4407 
4408     /**
4409      * Returns a hash code based on the contents of the specified array.
4410      * For any two {@code long} arrays {@code a} and {@code b}
4411      * such that {@code Arrays.equals(a, b)}, it is also the case that
4412      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4413      *
4414      * <p>The value returned by this method is the same value that would be
4415      * obtained by invoking the {@link List#hashCode() hashCode}
4416      * method on a {@link List} containing a sequence of {@link Long}
4417      * instances representing the elements of {@code a} in the same order.
4418      * If {@code a} is {@code null}, this method returns 0.
4419      *
4420      * @param a the array whose hash value to compute
4421      * @return a content-based hash code for {@code a}
4422      * @since 1.5
4423      */
4424     public static int hashCode(long a[]) {


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


4385         @Override
4386         public void forEach(Consumer<? super E> action) {
4387             Objects.requireNonNull(action);
4388             for (E e : a) {
4389                 action.accept(e);
4390             }
4391         }
4392 
4393         @Override
4394         public void replaceAll(UnaryOperator<E> operator) {
4395             Objects.requireNonNull(operator);
4396             E[] a = this.a;
4397             for (int i = 0; i < a.length; i++) {
4398                 a[i] = operator.apply(a[i]);
4399             }
4400         }
4401 
4402         @Override
4403         public void sort(Comparator<? super E> c) {
4404             Arrays.sort(a, c);
4405         }
4406 
4407         @Override
4408         public Iterator<E> iterator() {
4409             return new ArrayItr<>(a);
4410         }
4411     }
4412 
4413     private static class ArrayItr<E> implements Iterator<E> {
4414         private int cursor;
4415         private final E[] a;
4416 
4417         ArrayItr(E[] a) {
4418             this.a = a;
4419         }
4420 
4421         @Override
4422         public boolean hasNext() {
4423             return cursor < a.length;
4424         }
4425 
4426         @Override
4427         public E next() {
4428             int i = cursor;
4429             E[] a = this.a;
4430             if (i >= a.length)
4431                 throw new NoSuchElementException();
4432             cursor = i + 1;
4433             return a[i];
4434         }
4435     }
4436 
4437     /**
4438      * Returns a hash code based on the contents of the specified array.
4439      * For any two {@code long} arrays {@code a} and {@code b}
4440      * such that {@code Arrays.equals(a, b)}, it is also the case that
4441      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4442      *
4443      * <p>The value returned by this method is the same value that would be
4444      * obtained by invoking the {@link List#hashCode() hashCode}
4445      * method on a {@link List} containing a sequence of {@link Long}
4446      * instances representing the elements of {@code a} in the same order.
4447      * If {@code a} is {@code null}, this method returns 0.
4448      *
4449      * @param a the array whose hash value to compute
4450      * @return a content-based hash code for {@code a}
4451      * @since 1.5
4452      */
4453     public static int hashCode(long a[]) {


< prev index next >