Java Stream collect to LongSummaryStatistics via Collectors

Description

Java Stream collect to LongSummaryStatistics via Collectors

import java.util.Arrays;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.stream.Collectors;

public class Main {
   public static void main(String[] args) {
      Language[] langArray = { //
            new Language("CSS",  55555), //
            new Language("HTML", 66666), //
            new Language("Java", 44444.5), //
            new Language("Javascript", 77777.77), //
            new Language("C++", 88888), //
            new Language("C",   33333), //
            new Language("SQL", 99999.4) };//

      List<Language> langList = Arrays.asList(langArray);

      langList.stream().forEach(System.out::println);

      LongSummaryStatistics incomeStats = 
            langList//from   ww w  . ja va  2 s  .  c  o  m
            .stream()
            .collect(Collectors.summarizingLong(Language::getSalary));
      System.out.println(incomeStats);System.out.println(incomeStats);
   }
}

class Language {
   private String firstName;
   private double salary;

   public Language(String firstName, double salary) {
      this.firstName = firstName;
      this.salary = (long)salary;
   }

   public String getFirstName() {
      return firstName;
   }

   public long getSalary() {
      return (long)salary;
   }

   @Override
   public String toString() {
      return String.format("%-20s %8.2f", getFirstName(), getSalary());
   }
}



PreviousNext

Related