java8之stream实战

核心代码

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TotalDemo {

    public static void main(String[] args) {
        List<VideoOrder> videoOrders1 = Arrays.asList(
                new VideoOrder("20190242812", "springboot教程", 3),
                new VideoOrder("20194350812", "微服务SpringCloud", 5),
                new VideoOrder("20190814232", "Redis教程", 9),
                new VideoOrder("20190523812", "⽹⻚开发教程", 9),
                new VideoOrder("201932324", "百万并发实战Netty", 9));
        //总价 54
        List<VideoOrder> videoOrders2 = Arrays.asList(
                new VideoOrder("2019024285312", "springboot教程", 3),
                new VideoOrder("2019081453232", "Redis教程", 9),
                new VideoOrder("20190522338312", "⽹⻚开发教程", 9),
                new VideoOrder("2019435230812", "Jmeter压⼒测试", 5),
                new VideoOrder("2019323542411", "Git+Jenkins持续集成", 7),
                new VideoOrder("2019323542424", "Idea全套教程", 21));


        // 查找两个订单中相同的产品
        List<VideoOrder> collect = videoOrders1.stream().filter(videoOrders2::contains).collect(Collectors.toList());
        System.out.println(collect);


        // 两个订单中的不同产品
        List<VideoOrder> collect1 = videoOrders1.stream().filter(obj -> !videoOrders2.contains(obj)).collect(Collectors.toList());
        System.out.println(collect1);

        List<VideoOrder> collect2 = videoOrders2.stream().filter(obj -> !videoOrders1.contains(obj)).collect(Collectors.toList());
        System.out.println(collect2);


        List<VideoOrder> collect3 = videoOrders1.parallelStream().collect(Collectors.toList());
        collect3.addAll(videoOrders2);
        System.out.println(collect3);

        // 去重操作
        List<VideoOrder> collect4 = collect3.stream().distinct().collect(Collectors.toList());
        System.out.println(collect4);

        // 平均价格
        double v = videoOrders1.stream().collect(Collectors.averagingInt(VideoOrder::getMoney)).doubleValue();
        System.out.println(v);

        // 总价格
        int i = videoOrders1.stream().collect(Collectors.summingInt(VideoOrder::getMoney)).intValue();
        System.out.println(i);
    }
}

class VideoOrder {
    private String tradeNo;
    private int money;
    private String title;
    public VideoOrder(String tradeNo,String title, int money ){
        this.tradeNo = tradeNo;
        this.title = title;
        this.money = money;
    }
    public String getTradeNo() {
        return tradeNo;
    }
    public void setTradeNo(String tradeNo) {
        this.tradeNo = tradeNo;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "VideoOrder{" +
                "title='" + title + '\'' +
                '}';
    }

    @Override
    public int hashCode() {
        return title.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof  VideoOrder) {
            VideoOrder vo = (VideoOrder) obj;
            return title.equals(vo.title);
        }
        return super.equals(obj);
    }
}

你可能感兴趣的:(java8笔记,java,redis,spring,boot)