Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Dauda Ainoo
Added: Apr 26, 2020 8:11 AM
Views: 129
Tags: no tags
  1. import java.util.List;
  2. public class FilterMapToIntAndSum {
  3.         public static void main(String[] args) {
  4.                 List<User> list = User.getUsers();
  5.                 System.out.println("--- Sum of age between the user id 2 and 4 ---");
  6.                 int sum = list.stream().filter(u -> u.getId() >= 2 && u.getId() <= 4)
  7.                                 .mapToInt(u -> u.getAge()).sum();
  8.                 System.out.println("Sum: " + sum);
  9.         }
  10. }