Stream流操作示例


 1 private static double oneMoney;
 2     private static double twoMoney;
 3     private static double sumMoney;
 4 
 5     public static void main(String[] args) {
 6         List<Employee> list1 = new ArrayList<>();
 7         list1.add(new Employee("猪八戒", "男", 500.0, 100.0, null));
 8         list1.add(new Employee("孙悟空", "男", 500.0, 100.0, null));
 9         list1.add(new Employee("白骨精", "女", 600.0, 100.0, null));
10         list1.add(new Employee("蜘蛛精", "女", 500.0, 100.0, null));
11 
12         List<Employee> list2 = new ArrayList<>();
13         list2.add(new Employee("红孩儿", "男", 500.0, 100.0, null));
14         list2.add(new Employee("牛魔王", "男", 500.0, 100.0, null));
15         list2.add(new Employee("玉兔精", "女", 600.0, 100.0, null));
16         list2.add(new Employee("铁扇公主", "女", 500.0, 100.0, null));
17         list2.add(new Employee("小白龙", "男", 500.0, 100.0, null));
18 
19         //  开发一部最高工资
20         //  直接用 .map() 加工
21         TopperFormer top1 = list1.stream().max((e1, e2) -> Double.compare(e1.getWage() + e1.getBonus(), e2.getWage() + e2.getBonus()))
22                 .map(e -> new TopperFormer(e.getName(), e.getWage() + e.getBonus())).get();
23         System.out.println("开发一部最高工资:" + top1);
24 
25         //  总工资平均工资
26         list1.stream().sorted((e1, e2) -> Double.compare(e1.getWage() + e1.getBonus(), e2.getWage() + e2.getBonus()))
27                 .skip(1).limit(list1.size() - 2).forEach(e ->{
28                     oneMoney += e.getWage() + e.getBonus();
29                 });
30         System.out.println("开发一部的平均工资是:" + oneMoney / (list1.size() - 2));
31 
32         //  开发二部
33         //  开发二部最高工资
34         TopperFormer top2 = list2.stream().max((e1, e2) -> Double.compare(e1.getWage() + e1.getBonus(), e2.getWage() + e2.getBonus()))
35                 .map(e -> new TopperFormer(e.getName(), e.getWage() + e.getBonus())).get();
36         System.out.println("开发二部最高工资:" + top2);
37         //  总工资平均工资
38         list2.stream().sorted((e1, e2) -> Double.compare(e1.getWage() + e1.getBonus(), e2.getWage() + e2.getBonus()))
39                 .skip(1).limit(list2.size() - 2).forEach(e ->{
40                     twoMoney += e.getWage() + e.getBonus();
41                 });
42         System.out.println("开发二部的平均工资是:" + twoMoney / (list2.size() - 2));
43         
44         //  两个部门平均工资
45         Stream<Employee> s1 = list1.stream();
46         Stream<Employee> s2 = list2.stream();
47         Stream<Employee> s3 = Stream.concat(s1, s2);
48         s3.sorted((e1, e2) -> Double.compare(e1.getWage() + e1.getBonus(), e2.getWage() + e2.getBonus()))
49                 .skip(1).limit(list1.size() + list2.size() - 2).forEach(e ->{
50                     sumMoney += (e.getWage() + e.getBonus());
51                 });
52         
53         //  两个部门
54         //  BigDecimal 精确数字
55         BigDecimal a = BigDecimal.valueOf(sumMoney);
56         BigDecimal b = BigDecimal.valueOf(list1.size() + list2.size() - 2);
57         System.out.println("开发部的平均工资是:" + a.divide(b, 2, RoundingMode.HALF_UP));
58     }

 

posted @ 2024-01-25 18:15  小※兽  阅读(16)  评论(0)    收藏  举报