Example usage for java.util.stream Collectors averagingDouble

List of usage examples for java.util.stream Collectors averagingDouble

Introduction

In this page you can find the example usage for java.util.stream Collectors averagingDouble.

Prototype

public static <T> Collector<T, ?, Double> averagingDouble(ToDoubleFunction<? super T> mapper) 

Source Link

Document

Returns a Collector that produces the arithmetic mean of a double-valued function applied to the input elements.

Usage

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.of("1", "2", "3");

    double o = s.collect(Collectors.averagingDouble(n -> Double.parseDouble(n)));

    System.out.println(o);// ww w .ja  v  a2 s  .c o m
}

From source file:pl.prutkowski.java.playground.java8.TestCollectors.java

/**
 * @param args the command line arguments
 *///from   w  w  w . j a v  a2s  .  c om
public static void main(String[] args) {
    Map<String, Integer> monthByLen = months.stream()
            .collect(Collectors.toMap(String::toUpperCase, m -> StringUtils.countMatches(m, "e")));

    monthByLen.forEach((month, eCount) -> System.out.println(month + " -> " + eCount));

    System.out.println("---------------------------------");

    Map<Object, List<String>> monthByLen2 = months.stream()
            .collect(Collectors.groupingBy(m -> StringUtils.countMatches(m, "e")));

    monthByLen2.forEach((count, groupedMonths) -> System.out.println(count + " -> " + groupedMonths));

    System.out.println("---------------------------------");

    Double averageLength = months.stream().collect(Collectors.averagingDouble(String::length));
    System.out.println("Average length: " + averageLength);
    System.out.println("---------------------------------");

    Double max = months.stream().collect(Collectors.summarizingDouble(String::length)).getMax();
    System.out.println("Max length: " + max);
    System.out.println("---------------------------------");

    String reduced = months.stream().collect(Collectors.reducing((m1, m2) -> (m1 + ", " + m2))).get();
    System.out.println("Reduced: " + reduced);
    System.out.println("---------------------------------");
    System.out.println(String.join(", ", months));
    System.out.println("---------------------------------");

    List<String> monthsWithZ = months.stream().filter(m -> m.contains("z")).collect(new ListCollector<>());
    System.out.println(monthsWithZ);

}