stream流 -初

Stream 流

eg:

菜肴:Dish.java

@Data
public class Dish {
    private String name;
    private boolean vegetarian;
    private int calories;
    private Type type;

    public Dish(){}

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

类型:Type.java

@Data
public class Type {

    private  String typeName;

    private Type(){}
    public Type(String typeName){
        this.typeName= typeName;
    }
}

test.java

 @Test
    void contextLoads() {
        List<Dish> dishList =  new ArrayList<>();
        dishList.add(new Dish("红烧肉",false,600,new Type("肉菜")));
        dishList.add(new Dish("红烧鱼",false,10000,new Type("肉菜")));
        dishList.add(new Dish("佛跳墙",false,20000,new Type("肉菜")));
        dishList.add(new Dish("香菇青菜",false,200,new Type("素菜")));
        dishList.add(new Dish("醋溜白菜",false,100,new Type("素菜")));

     System.out.println(afterJava8(dishList).toString());
        
    }

private List<String> afterJava8(List<Dish> dishList) {
    return dishList.stream()
            .filter(d -> d.getCalories() < 400) //筛选出卡路里小于400的菜肴
            .sorted(comparing(Dish::getCalories)) //根据卡路里进行排序(升序)
            .map(Dish::getName) //提取菜肴名称
            .collect(Collectors.toList()); //转换为List
}

生成流的5种方式

  1. 通过集合

    List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
    Stream<Integer> stream = integerList.stream();
    
  2. 通过数组

    int[] intArr = new int[]{1, 2, 3, 4, 5};
    IntStream stream = Arrays.stream(intArr);
    
  3. 通过值

    Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
    
  4. 通过文件生成

    Stream<String> lines = Files.lines(Paths.get("data.txt"), Charset.defaultCharset())
    
  5. 通过函数生成 提供了iterate和generate两个静态方法从函数中生成流

    Stream<Integer> stream = Stream.iterate(0, n -> n + 2).limit(5);
    Stream<Double> stream = Stream.generate(Math::random).limit(5);
    

使用的方法

filter筛选

List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);
Stream<Integer> stream = integerList.stream().filter(i -> i > 3);

distinct去除重复元素

List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);
Stream<Integer> stream = integerList.stream().distinct();

limit返回指定流个数

List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);
Stream<Integer> stream = integerList.stream().limit(3);

skip跳过流中的元素

List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);
Stream<Integer> stream = integerList.stream().skip(2);

统计流中元素个数

1.通过count

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
Long result = integerList.stream().count();

2.通过counting

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
Long result = integerList.stream().collect(counting());

查询

1.findFirst查找第一个

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> result = integerList.stream().filter(i -> i > 3).findFirst();

2.findAny随机查找一个

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> result = integerList.stream().filter(i -> i > 3).findAny();

查询结果一样

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值