stream式编程入门

stream式编程入门

实体类


public class Apple {
    private int id;
    private String color;
    private int weight;
    private String origin;

    public Apple(int id, String color, int weight, String origin) {
        this.id = id;
        this.color = color;
        this.weight = weight;
        this.origin = origin;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "id=" + id +
                ", color='" + color + '\'' +
                ", weight=" + weight +
                ", origin='" + origin + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }
}

Lambda表达式


public class  SimpleLambda {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello");
            }
        }).start();
        new Thread(()-> System.out.println("hello world")).start();
        /**
         * 参数特性:
         * 可以忽略 类型
         * 参数只有一个情况 ,可以省略括号
         * 代码编写特性:
         * 1.单行表达式 省略掉return
         * 2.代码块加return
         * 3.方法引用
         */
        start((name,age) -> name+age);
        start(((name, age) -> {
            return "name"+name;
        }));
        //静态
        start(SimpleLambda::doFormat);
        //普通
        start(new SimpleLambda()::doFormat2);
        //前置条件,必须是函数式接口
        //参数的传递
        //代码便携方式
    }

    public static String doFormat(String param2,int age){
        return "name"+param2;
    }
    public  String doFormat2(String param2,int age){
        return "name"+param2;
    }
    public static void start(myRun2 myRun2){
//        new Thread(myRun2).start();
        myRun2.run("w",22);
    }

    //1.只能一个方法--》函数式接口
    //2.默认方法除外
    //3.Object 下面的方法除外
    @FunctionalInterface
    public interface myRun2 /*extends  Runnable*/{
        String run(String name,Integer age);
        String toString();
        boolean equals(Object obj);
    }
}

stream


import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AppleStore {
    private static List<Apple> appleStore=new ArrayList<>();
    static {
        appleStore.add(new Apple(1,"red",500,"湖南"));
        appleStore.add(new Apple(2,"red",400,"湖南"));
        appleStore.add(new Apple(3,"green",300,"湖南"));
        appleStore.add(new Apple(4,"green",200,"天津"));
        appleStore.add(new Apple(5,"green",100,"天津"));
    }

    //找出红色的苹果
    //重量
    //产地
    public void testq(){
        for (Apple apple : appleStore) {
            if (apple.getColor().equals("red")) {
                //add
            }
        }
    }
    //过滤一个颜色
    public void test2(){
        List<Apple> red = appleStore.stream()
                .filter(a -> a.getColor().equals("red"))
                .filter(a->a.getWeight()>300)
                .collect(Collectors.toList());
    }
    //过滤一个颜色
    public void test3(Predicate<? super Apple> pr){
        List<Apple> red = appleStore.stream()
                .filter(pr)
                .collect(Collectors.toList());
        System.out.println(red);
    }

    //求出每个颜色的平均重量
    public void test4(){
        // 1.基于颜色分组
        Map<String ,List<Apple>> maps=new HashMap<>();
        for (Apple apple : appleStore) {
            //如果list不存在就创建一个
            List<Apple> apples = maps.computeIfAbsent(apple.getColor(), key -> new ArrayList<Apple>());
            apples.add(apple);
        }
    }
    public void test5(){
       appleStore.stream()
                .collect(Collectors.groupingBy(a -> a.getColor(), //基于颜色分组
                        Collectors.averagingInt(a -> a.getWeight()))) //统计平均重量
               .forEach((key,value)-> System.out.println(key+":"+value));
    }
    public static void main(String[] args) {
        new AppleStore().test3(a->a.getColor().equals("red")&&a.getWeight()>300);
        new AppleStore().test5();

        /*
         * 流的生成 与不可重复使用
         *  Arrays.stream(new int[]{1,2,3,4});
            Stream.of(1,2,3,4,5);
         *
         */
        Stream<Apple> stream1 = appleStore.stream();
        Stream<Apple> stream2 = stream1.filter(a -> a.getColor().equals("red"));
        //Stream<Apple> stream3 = stream1.filter(a -> a.getWeight()>100); //报错流使用过就会close
        //中间节点-》懒节点
        appleStore.stream()

                //中间节点
                .filter(a->{

            //过滤
            return a.getColor().equals("red")||a.getColor().equals("green");
        })

                //流是一个一个执行的
//               .peek(a-> System.out.println(a.getColor()))
//               .peek(a-> System.out.println(a.getWeight()))

                /**
                 * 影响方式:
                 * 1。过滤
                 * 2。转换
                 */
                //转换-》String
                .map(a->a.getColor())
                //去重
                .distinct()
                .peek(a-> System.out.println(a))

                //终值节点
                .toArray();
        /**
         * 采集
         * 1.list
         * 2.map
         * 3.group by
         * 4.数组
         * 5.求出最大值
         * 6.求任意值
         */

        //基于颜色分组
        Map<Integer, Apple> collect = appleStore.stream().collect(Collectors.toMap(a -> a.getId(), a -> a));
        collect.entrySet().forEach(k->{
            System.out.println(k);
        });


    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值