Java入门与基础(stream函数式编程代码示例)


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

public class StreamTest {
    private static final List<Apple> APPLES = create();

    private static List<Apple> create() {
        Apple a = new Apple(1, Apple.Color.RED, 200);
        Apple a2 = new Apple(2, Apple.Color.GREEN, 250);
        Apple a3 = new Apple(3, Apple.Color.RED, 260);
        Apple a4 = new Apple(4, Apple.Color.GREEN, 230);
        return List.of(a, a2, a3, a4);
    }

    public static void main(String[] args) {
        // forEach();
        /*filter(Apple.Color.RED)
                .forEach(a -> System.out.println(a.getId()));*/
        // map();
        // sorted();
        // sortedReversed();
        // collect();
        // collectGroupingBy2();
    }

    /**
     * foreach循环
     * forEach()方法,简写
     */
    private static void forEach() {
        for (Apple a : APPLES) {
            System.out.println(a.getWeight());
        }
        APPLES.forEach(a -> System.out.println(a.getWeight()));
    }

    /**
     * 原实现。返回指定颜色的全部苹果
     */
    private static List<Apple> getRedApples(Apple.Color c) {
        List<Apple> reds = new ArrayList<>();
        for (Apple a : APPLES) {
            if (c == a.getColor()) {
                reds.add(a);
            }
        }
        return reds;
    }

    /**
     * 基于stream的过滤
     * 修改方法返回值,方法链
     * 比较源/新集合
     *
     * @param c
     */
    private static List<Apple> filter(Apple.Color c) {
        return APPLES.stream()
                .filter(a -> a.getColor() == c)
                .collect(Collectors.toList());
    }

    /**
     * 过滤指定颜色,且大于等于指定重量的苹果。2次过滤
     *
     * @param c
     * @param weight
     */
    private static void filter(Apple.Color c, int weight) {
        APPLES.stream()
                .filter(a -> a.getColor() == c)
                .filter(a -> a.getWeight() >= weight)
                .collect(Collectors.toList())
                .forEach(a -> System.out.println(a.getId()));

    }


    /**
     * 将重量映射为新集合,遍历打印
     * 简写
     */
    private static void map() {
        APPLES.stream()
                .map(Apple::getWeight)
                .collect(Collectors.toList())
                .forEach(System.out::println);

    }

    /**
     * 以重量顺序排序
     */
    private static void sorted() {
        APPLES.stream()
                .sorted(Comparator.comparing(Apple::getWeight))
                .collect(Collectors.toList())
                .forEach(a -> System.out.println(a.getWeight()));
    }

    /**
     * 以ID逆序排序
     */
    private static void sortedReversed() {
        APPLES.stream()
                .sorted(Comparator.comparing(Apple::getId).reversed())
                .collect(Collectors.toList())
                .forEach(a -> System.out.println(a.getId()));
    }

    /**
     * 获取颜色,分别按list/set聚合
     */
    private static void collect() {
        APPLES.stream()
                .map(Apple::getColor)
                .collect(Collectors.toList())
                .forEach(System.out::println);

    }

    /**
     * Map操作。基于颜色分组
     */
    private static void collectGroupingBy() {
        Map<Apple.Color, List<Apple>> map = APPLES.stream()
                .collect(Collectors.groupingBy(Apple::getColor));

    }

    /**
     * 以ID为键,苹果对象为值。自定义键值对
     * forEach遍历
     */
    private static void collectGroupingBy2() {
        APPLES.stream()
                .collect(Collectors.toMap(Apple::getId, a -> a))
                .forEach((k, v) -> {
                    System.out.println(k + "/" + v.getColor());
                });

    }

    /**
     * 函数中,只能操作函数体外的常量
     */
    private static void finalValue() {
       int count = 0;
        APPLES.forEach(a -> {
            System.out.println(count);
        });
    }

    /**
     * 2组过滤相同
     */
    private static void getFilter() {
        Apple a1 = new Apple(1, Apple.Color.RED, 200);
        Apple a2 = new Apple(5, Apple.Color.GREEN, 240);
        List<Apple> newApples = List.of(a1, a2);
        List<Apple> oldApples = APPLES;
        List<Apple> apples = new ArrayList<>();
        for (Apple a : newApples) {
            for (Apple oa : oldApples) {
                if (a.getId() == oa.getId()) {
                    apples.add(a);
                }
            }
        }
        apples.forEach(a -> System.out.println(a.getId()));
        System.out.println("--------------");
        List<Apple> apples2 = newApples.stream()
                .filter(a -> oldApples.stream()
                        .anyMatch(oa -> oa.getId() == a.getId()))
                .collect(Collectors.toList());

        apples2.forEach(a -> System.out.println(a.getId()));
    }
}
package stream;


public class Apple {
    public enum Color{
        RED, GREEN
    }
    private int id;
    private Color color;
    private int weight;
    public Apple(int id, Color color, int weight) {
        this.id = id;
        this.color = color;
        this.weight = weight;
    }

    public int getId() {
        return id;
    }

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

    public Color getColor() {
        return color;
    }

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

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值