Java 的 Stream

一、创建 Stream

1.1、创建 Stream 流

1.1.1、List 集合获取 Stream 流

Collection list = new ArrayList<>();
Stream s1 = list.stream();

1.1.2、Map 集合获取 stream 流

Map map = new HashMap<>();
// 键流
Stream s2 = map.keySet().stream();
// 值流
Stream s3 = map.values().stream();
// 键值对
Stream> s4 = map.entrySet().stream();

1.1.3、数组获取 Stream 流

String[] strs = {"A", "B", "C"};
Stream s5 = Arrays.stream(strs);
Stream s6 = Stream.of(strs);

1.2、方法分类

1.2.1、中间方法

Java 的 Stream_第1张图片

1.2.2、终结方法

1.2.3、收集方法 

Java 的 Stream_第2张图片

 二、代码操作

public class MyUser {

   private Integer id;

   private String name;

   private Integer score;

   private List menus;

   public MyUser() {}

   public MyUser(Integer id, String name, Integer score, List menus) {
        this.id = id;
        this.name = name;
        this.score = score;
        this.menus = menus;
   }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public List getMenus() {
        return menus;
    }

    public void setMenus(List menus) {
        this.menus = menus;
    }

    @Override
    public String toString() {
        return "Merchant{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", score=" + score +
                ", menus=" + menus +
                '}';
    }
}
    public static void main(String[]args){
        MyUser myUser1 = new MyUser(1,"AA",11, Arrays.asList("A","B","C"));
        MyUser myUser2 = new MyUser(2,"BB",22, Arrays.asList("D","E","F"));
        MyUser myUser3 = new MyUser(3,"CC",19, Arrays.asList("O","P","H"));
        MyUser myUser4 = new MyUser(4,"DD",33, Arrays.asList("K","J","I"));
        MyUser myUser5 = new MyUser(5,"EE",39, Arrays.asList("X","M","N"));

        List list = Arrays.asList(myUser1,myUser2,myUser3,myUser4,myUser5);

        // 遍历p
        list.stream().forEach(
                s -> System.out.println(s)
        );
        // 取最大值、最小值
        Comparator comparator = Comparator.comparing(MyUser::getScore);
        Optional max = list.stream().max(comparator);
        Optional min = list.stream().min(comparator);
        System.out.println("max: "+max);
        System.out.println("min: "+min);

        // 单个属性加减乘除,生成新集合。
        List collect1 = list.stream().map(x -> {
            x.setScore(x.getScore()+100);
            return x;
        }).collect(Collectors.toList());
        System.out.println("collect1: "+collect1);
        // 过滤元素 对原集合的成员的属性修改
        list.stream().filter(x->x.getId()>2).
                forEach(x -> {
                    x.setName(x.getName()+100);
                });
        System.out.println("list22: "+list);

        // 统计
        //统计员工人数
        Long count = list.stream().collect(Collectors.counting());
        //求平均工资
        Double average = list.stream().collect(Collectors.averagingDouble(MyUser::getScore));
        //求最高工资
        Optional max2 = list.stream().map(MyUser::getScore).collect(Collectors.maxBy(Integer::compare));
        //求工资之和
        Integer sum = list.stream().collect(Collectors.summingInt(MyUser::getScore));
        //一次性统计所有信息
        DoubleSummaryStatistics collect = list.stream().collect(Collectors.summarizingDouble(MyUser::getScore));
        System.out.println("统计员工人数:"+count);
        System.out.println("求平均工资:"+average);
        System.out.println("求最高工资:"+max2);
        System.out.println("求工资之和:"+sum);
        System.out.println("一次性统计所有信息:"+collect);

        // 过滤后 转为 Map
        Map map =list.stream().filter(s->s.getId()>3).collect(Collectors.toMap(s->s.getId(),y->y));
        System.out.println("map:"+map);
    }

 

你可能感兴趣的:(java,python,前端)