deyanmalinov

2. Sum Numbers

Jun 4th, 2019
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.function.Function;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.     public static void main(String[] args){
  11.         Scanner scan = new Scanner(System.in);
  12.         String[] line = scan.nextLine().split(", ");
  13.         Function<String, Integer> parse = Integer::parseInt;
  14.  
  15.         List<Integer> nums = Arrays.stream(line)
  16.                 .map(parse)
  17.                 .collect(Collectors.toList());
  18.         System.out.println("Count = " + nums.size());
  19.  
  20.         int sum = nums.stream()
  21.                 .reduce(0, (a, b) -> a+b);
  22.         System.out.println("Sum = " + sum);
  23.     }
  24. }
RAW Paste Data