Java 8 stream 流获取 list 中最大值、最小值、总和值、平均值

你的名字 2022-12-10 10:18 932阅读 0赞

模拟普通对象输出:

  1. // list
  2. static List<Employee> empList = new ArrayList<Employee>();
  3. private static void initEmp(){
  4. empList.add(new Employee("张三",30));
  5. empList.add(new Employee("张三1",96));
  6. empList.add(new Employee("张三2",23));
  7. empList.add(new Employee("张三3",69));
  8. empList.add(new Employee("张三4",85));
  9. empList.add(new Employee("张三5",62));
  10. empList.add(new Employee("张三6",12));
  11. empList.add(new Employee("张三7",99));
  12. empList.add(new Employee("张三8",11));
  13. }
  14. //获取员工年龄的最大、最小、总和、平均值
  15. public static void main(String[] args) {
  16. initEmp();
  17. int sum = empList.stream().mapToInt(Employee->Employee.getAge()).sum();
  18. int max = empList.stream().mapToInt(Employee->Employee.getAge()).max().getAsInt();
  19. int min = empList.stream().mapToInt(Employee->Employee.getAge()).min().getAsInt();
  20. double avg = empList.stream().mapToInt(Employee->Employee.getAge()).average().getAsDouble();
  21. System.out.println("最大值:"+max+"\n最小值:"+min+"\n总和:"+sum+"\n平均值:"+avg);

模拟SQL输出:

  1. import org.testng.collections.Lists;
  2. import org.testng.collections.Maps;
  3. import java.util.List;
  4. import java.util.Map;
  5. /** * @Class: TestStream * @Description: * @Author: Jerry(姜源) * @Create: 2020/9/25 16:14 */
  6. public class TestStream {
  7. public static void main(String[] args) {
  8. List<Map> list = initData();
  9. // 取SQL中 number列 的最大值
  10. int max = list.stream().mapToInt(
  11. m -> m.get("number") != null ? Integer.parseInt(m.get("number").toString()) : 0
  12. ).max().getAsInt();
  13. System.err.println("max = " + max); //22
  14. // 取SQL中 number列 的最小值
  15. int min = list.stream().mapToInt(
  16. m -> m.get("number") != null ? Integer.parseInt(m.get("number").toString()) : 0
  17. ).min().getAsInt();
  18. System.err.println("min = " + min); //1
  19. // 取SQL中 number列 的总和值
  20. int sum = list.stream().mapToInt(
  21. m -> m.get("number") != null ? Integer.parseInt(m.get("number").toString()) : 0
  22. ).sum();
  23. System.err.println("sum = " + sum); //26
  24. // 取SQL中 number列 的平均值
  25. double avg = list.stream().mapToInt(
  26. m -> m.get("number") != null ? Integer.parseInt(m.get("number").toString()) : 0
  27. ).average().getAsDouble();
  28. System.err.println("avg = " + avg); //8.666666666666666
  29. }
  30. /** * 模拟 SQL 的输出 * test number * A 1 * B 22 * C 3 */
  31. public static List<Map> initData() {
  32. List<Map> list = Lists.newLinkedList();
  33. Map map = Maps.newLinkedHashMap();
  34. map.put("test", "A");
  35. map.put("number", 1);
  36. list.add(map);
  37. Map map1 = Maps.newLinkedHashMap();
  38. map1.put("test", "B");
  39. map1.put("number", 22);
  40. list.add(map1);
  41. Map map2 = Maps.newLinkedHashMap();
  42. map2.put("test", "C");
  43. map2.put("number", 3);
  44. list.add(map2);
  45. System.err.println("sql result = " + list);
  46. return list;
  47. }
  48. }

发表评论

表情:
评论列表 (有 0 条评论,932人围观)

还没有评论,来说两句吧...

相关阅读