< prev index next >

src/java.base/share/classes/java/util/stream/Collectors.java

Print this page


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


1865     }
1866 
1867     /**
1868      * Returns a {@code Collector} which applies an {@code double}-producing
1869      * mapping function to each input element, and returns summary statistics
1870      * for the resulting values.
1871      *
1872      * @param <T> the type of the input elements
1873      * @param mapper a mapping function to apply to each element
1874      * @return a {@code Collector} implementing the summary-statistics reduction
1875      *
1876      * @see #summarizingLong(ToLongFunction)
1877      * @see #summarizingInt(ToIntFunction)
1878      */
1879     public static <T>
1880     Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) {
1881         return new CollectorImpl<T, DoubleSummaryStatistics, DoubleSummaryStatistics>(
1882                 DoubleSummaryStatistics::new,
1883                 (r, t) -> r.accept(mapper.applyAsDouble(t)),
1884                 (l, r) -> { l.combine(r); return l; }, CH_ID);
















































































1885     }
1886 
1887     /**
1888      * Implementation class used by partitioningBy.
1889      */
1890     private static final class Partition<T>
1891             extends AbstractMap<Boolean, T>
1892             implements Map<Boolean, T> {
1893         final T forTrue;
1894         final T forFalse;
1895 
1896         Partition(T forTrue, T forFalse) {
1897             this.forTrue = forTrue;
1898             this.forFalse = forFalse;
1899         }
1900 
1901         @Override
1902         public Set<Map.Entry<Boolean, T>> entrySet() {
1903             return new AbstractSet<>() {
1904                 @Override
   1 /*
   2  * Copyright (c) 2012, 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


1865     }
1866 
1867     /**
1868      * Returns a {@code Collector} which applies an {@code double}-producing
1869      * mapping function to each input element, and returns summary statistics
1870      * for the resulting values.
1871      *
1872      * @param <T> the type of the input elements
1873      * @param mapper a mapping function to apply to each element
1874      * @return a {@code Collector} implementing the summary-statistics reduction
1875      *
1876      * @see #summarizingLong(ToLongFunction)
1877      * @see #summarizingInt(ToIntFunction)
1878      */
1879     public static <T>
1880     Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) {
1881         return new CollectorImpl<T, DoubleSummaryStatistics, DoubleSummaryStatistics>(
1882                 DoubleSummaryStatistics::new,
1883                 (r, t) -> r.accept(mapper.applyAsDouble(t)),
1884                 (l, r) -> { l.combine(r); return l; }, CH_ID);
1885     }
1886 
1887     /**
1888      * Returns a {@code Collector} that passes the input elements to two specified collectors
1889      * and merges their results with the specified merge function.
1890      *
1891      * <p>The resulting collector is {@link Collector.Characteristics#UNORDERED} if both supplied
1892      * collectors are unordered and {@link Collector.Characteristics#CONCURRENT} if both supplied
1893      * collectors are concurrent.
1894      *
1895      * @param <T>    the type of the input elements
1896      * @param <R1>   the result type of the first collector
1897      * @param <R2>   the result type of the second collector
1898      * @param <R>    the final result type
1899      * @param c1     the first collector
1900      * @param c2     the second collector
1901      * @param merger the function which merges two results into the single one
1902      * @return a {@code Collector} which aggregates the results of two supplied collectors.
1903      * @since 12
1904      */
1905     public static <T, R1, R2, R>
1906     Collector<T, ?, R> teeingAndThen(Collector<? super T, ?, R1> c1,
1907                                      Collector<? super T, ?, R2> c2,
1908                                      BiFunction<? super R1, ? super R2, ? extends R> merger) {
1909         return teeingAndThen0(c1, c2, merger);
1910     }
1911 
1912     private static <T, A1, A2, R1, R2, R>
1913     Collector<T, ?, R> teeingAndThen0(Collector<? super T, A1, R1> c1,
1914                                       Collector<? super T, A2, R2> c2,
1915                                       BiFunction<? super R1, ? super R2, ? extends R> merger) {
1916         Objects.requireNonNull(c1, "c1");
1917         Objects.requireNonNull(c2, "c2");
1918         Objects.requireNonNull(merger, "merger");
1919 
1920         Supplier<A1> c1Supplier = Objects.requireNonNull(c1.supplier(), "c1 supplier");
1921         Supplier<A2> c2Supplier = Objects.requireNonNull(c2.supplier(), "c2 supplier");
1922         BiConsumer<A1, ? super T> c1Accumulator = Objects.requireNonNull(c1.accumulator(), "c1 accumulator");
1923         BiConsumer<A2, ? super T> c2Accumulator = Objects.requireNonNull(c2.accumulator(), "c2 accumulator");
1924         BinaryOperator<A1> c1Combiner = Objects.requireNonNull(c1.combiner(), "c1 combiner");
1925         BinaryOperator<A2> c2Combiner = Objects.requireNonNull(c2.combiner(), "c2 combiner");
1926         Function<A1, R1> c1Finisher = Objects.requireNonNull(c1.finisher(), "c1 finisher");
1927         Function<A2, R2> c2Finisher = Objects.requireNonNull(c2.finisher(), "c2 finisher");
1928 
1929         Set<Collector.Characteristics> characteristics;
1930         Set<Collector.Characteristics> c1Characteristics = c1.characteristics();
1931         Set<Collector.Characteristics> c2Characteristics = c2.characteristics();
1932         if (CH_ID.containsAll(c1Characteristics) || CH_ID.containsAll(c2Characteristics)) {
1933             characteristics = CH_NOID;
1934         } else {
1935             EnumSet<Collector.Characteristics> c = EnumSet.noneOf(Collector.Characteristics.class);
1936             c.addAll(c1Characteristics);
1937             c.retainAll(c2Characteristics);
1938             c.remove(Collector.Characteristics.IDENTITY_FINISH);
1939             characteristics = Collections.unmodifiableSet(c);
1940         }
1941 
1942         class PairBox {
1943             A1 left = c1Supplier.get();
1944             A2 right = c2Supplier.get();
1945 
1946             void add(T t) {
1947                 c1Accumulator.accept(left, t);
1948                 c2Accumulator.accept(right, t);
1949             }
1950 
1951             PairBox combine(PairBox other) {
1952                 left = c1Combiner.apply(left, other.left);
1953                 right = c2Combiner.apply(right, other.right);
1954                 return this;
1955             }
1956 
1957             R get() {
1958                 R1 r1 = c1Finisher.apply(left);
1959                 R2 r2 = c2Finisher.apply(right);
1960                 return merger.apply(r1, r2);
1961             }
1962         }
1963 
1964         return new CollectorImpl<>(PairBox::new, PairBox::add, PairBox::combine, PairBox::get, characteristics);
1965     }
1966 
1967     /**
1968      * Implementation class used by partitioningBy.
1969      */
1970     private static final class Partition<T>
1971             extends AbstractMap<Boolean, T>
1972             implements Map<Boolean, T> {
1973         final T forTrue;
1974         final T forFalse;
1975 
1976         Partition(T forTrue, T forFalse) {
1977             this.forTrue = forTrue;
1978             this.forFalse = forFalse;
1979         }
1980 
1981         @Override
1982         public Set<Map.Entry<Boolean, T>> entrySet() {
1983             return new AbstractSet<>() {
1984                 @Override
< prev index next >