Beispiel #1
0
  public static void main(String[] args) {

    out.println("Gauss: " + MAX * (MAX + 1) / 2); // 500000500000

    long sum = 0L;
    for (long i = 0; i <= MAX; i++) {
      sum = sum + i;
    }
    out.println("For loop: " + sum);

    Long another =
        Stream.iterate(0L, l -> ++l).limit(MAX + 1).collect(Collectors.summingLong(l -> l));

    out.println("With a Stream of Long and Collectors.summingLong: " + another);
  }
  public static void main(String[] args) {

    long total = colaboradores.stream().filter(c -> c.getSalario() > 5000).count();

    System.out.println("total : " + total);

    System.out.println("Nomes dos ricos\n");
    colaboradores
        .stream()
        .filter(c -> c.getSalario() > 5000)
        .map(c -> c.getNome())
        .forEach(System.out::println);

    System.out.println("\nCpf dos ricos\n");

    List<String> cpfs =
        colaboradores
            .stream()
            .filter(c -> c.getSalario() > 5000)
            .map(c -> c.getCpf())
            .collect(Collectors.toList());

    cpfs.forEach(System.out::println);

    System.out.println("\nRicos completos\n");

    List<Colaborador> ricos =
        colaboradores.stream().filter(c -> c.getSalario() > 5000).collect(Collectors.toList());

    ricos.forEach(System.out::println);

    System.out.println("\nCpf dos ricos\n");

    double totalSalarios =
        colaboradores
            .stream()
            .filter(c -> c.getSalario() > 5000)
            .collect(Collectors.summingDouble(Colaborador::getSalario));

    System.out.println("\nTotal dos salários " + totalSalarios);

    long totalId =
        colaboradores
            .stream()
            .filter(c -> c.getSalario() > 5000)
            .collect(Collectors.summingLong(Colaborador::getId));

    System.out.println("\nTotal dos ids " + totalId);

    System.out.println("\nExibindo por ordem alfabética os nomes\n");

    colaboradores
        .stream()
        .filter(c -> c.getSalario() > 5000)
        .map(c -> c.getNome().toUpperCase())
        .sorted()
        .forEach(System.out::println);

    colaboradores
        .stream()
        .filter(c -> c.getSalario() > 5000)
        .map(c -> c.getNome().toUpperCase())
        .sorted(Comparator.comparing(String::toUpperCase))
        .forEach(System.out::println);

    System.out.println("\nExibindo por ordem alfabética inversa os nomes\n");

    colaboradores
        .stream()
        .filter(c -> c.getSalario() > 5000)
        .sorted(Comparator.comparing(Colaborador::getNome).reversed())
        .map(c -> c.getNome().toUpperCase())
        .forEach(System.out::println);
  }
 // public static AggregationDescriptor<Object, Object, Long> buildCount() {
 // return new AggregationDescriptor<Object, Object,
 // Long>(AggregationDescriptor.Operation.COUNT);
 // }
 //
 public static <A> Collector<A, ?, Long> buildSum(ToLongFunction<A> extractor) {
   return Collectors.<A>summingLong(extractor);
   // return AggregationDescriptor.SUM.apply(extractor);
 }