java8 Stream API各种流操作应用(2)

Stream API大概有筛选、切片、映射、查找、匹配和归约等操作,现在我们都实际应用下

import java.util.*;

public class Dish {

    private final String name;
    private final boolean vegetarian;
    private final int calories;
    private final Type type;

    public Dish(String name, boolean vegetarian, int calories, Type type) {
        this.name = name;
        this.vegetarian = vegetarian;
        this.calories = calories;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public int getCalories() {
        return calories;
    }

    public Type getType() {
        return type;
    }

    public enum Type { MEAT, FISH, OTHER }

    @Override
    public String toString() {
        return name;
    }

    public static final List<Dish> menu =
            Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT),
                           new Dish("beef", false, 700, Dish.Type.MEAT),
                           new Dish("chicken", false, 400, Dish.Type.MEAT),
                           new Dish("french fries", true, 530, Dish.Type.OTHER),
                           new Dish("rice", true, 350, Dish.Type.OTHER),
                           new Dish("season fruit", true, 120, Dish.Type.OTHER),
                           new Dish("pizza", true, 550, Dish.Type.OTHER),
                           new Dish("prawns", false, 400, Dish.Type.FISH),
                           new Dish("salmon", false, 450, Dish.Type.FISH));
}



filter筛选

//筛选
		//List<Dish> vegetarianMenu = Dish.menu.stream().filter(Dish::isVegetarian).collect(toList());
		Dish.menu.stream().filter(Dish::isVegetarian).collect(toList()).forEach(System.out::println);

List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
		numbers.stream().filter(i -> i % 2 == 0).distinct().forEach(System.out::println);

limit截短流

//limit截短流
		Dish.menu.stream().filter(d -> d.getCalories() > 300).limit(3).collect(toList()).forEach(System.out::println);


//skip跳过前面元素
		Dish.menu.stream().filter(d -> d.getCalories() > 300).skip(3).collect(toList()).forEach(System.out::println);



//映射  应getName类型String,map输出流类型Stream<String>
		Dish.menu.stream().map(Dish::getName).collect(toList()).forEach(System.out::println);



//菜单中每个名称长度
		Dish.menu.stream().map(Dish::getName).map(String::length).collect(toList()).forEach(System.out::println);


//flatMap流的扁平化:单个numbers2.stream().map流合成一个流,代码功能:将2个数组合并新的数组并过滤将各个数组中的单个元素之和被3整除后的结果
		List<Integer> numbers1 = Arrays.asList(1, 2, 3);
		List<Integer> numbers2 = Arrays.asList(3, 4);
		List<Integer[]> pairs = numbers1.stream().flatMap(i -> { // System.out.println(i+"--");
			return numbers2.stream().filter(j -> (i + j) % 3 == 0).map(j -> {
				//System.out.println(i + " " + j);
				return new Integer[] { i, j };
			});
		}).collect(toList());

// 匹配anyMatch,allMatch,noneMatch
		// anyMatch谓词 Dish.menu.stream是否有一个匹配 anyMatch谓词
		if (Dish.menu.stream().anyMatch(Dish::isVegetarian)) {
			System.out.println("The menu is (somewhat) vegetarian friendly!!");
		}

		// allMatch Dish.menu.stream是否都能匹配 allMatch谓词
		if (Dish.menu.stream().allMatch(Dish::isVegetarian)) {
			System.out.println("The menu is (somewhat) vegetarian friendly!!");
		}

		// noneMatch Dish.menu.stream没有匹配 noneMatch谓词
		if (Dish.menu.stream().noneMatch(Dish::isVegetarian)) {
			System.out.println("The menu is (somewhat) vegetarian friendly!!");
		}

		// 查找元素findAny,findFirst
		// findAny返回当前流中的任意元素
		// Dish.menu.stream().filter(Dish::isVegetarian).findAny();
		// ifPresent是否返回一个
		Dish.menu.stream().filter(Dish::isVegetarian).findAny().ifPresent(d -> System.out.println(d.getName()));
		Optional<Integer> firstSquareDivisibleByThree = Arrays.asList(1, 2, 3, 4, 5).stream().map(x -> x * x).filter(x -> x % 3 == 0)
				.findFirst(); // 9



// 归约reduce
		// int count = Dish.menu.stream().map(d -> 1).reduce(0, (a, b) -> a +
		// b);
		System.out.println(Dish.menu.stream().map(d -> 1).reduce(0, (a, b) -> {
			//System.out.println(a+" "+b);
			return a + b;
		}));
		//reduce计算形式  1*5*2*3
		System.out.println(Arrays.asList(5,2,3).stream().reduce(1, (a, b) -> { System.out.println(a+" "+b);  return a * b;}));
		//reduce计算形式 0+5+2+3
		System.out.println(Arrays.asList(5,2,3).stream().reduce(0, Integer::sum));
     
		//5 重复lambda参数比较 计算形式  0,5-->5  5,2-->5   5,3-->5  
		System.out.println(Arrays.asList(5,2,3).stream().reduce(0, Integer::max));
		
		//0  重复lambda参数比较 计算形式  0,5-->0  0,2-->0   0,3-->0  
		System.out.println(Arrays.asList(5,2,3).stream().reduce(0, Integer::min));




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luozhonghua2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值