java8的Stream基本用法

package com.two.zero.one.eight.december;

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

/**
 * @description: Stream操作
 * @author: mao.li
 * @date 2018/12/08 16:38
 */
public class StreamOperation {

    public static void main(String[] args) {

        Student a = new Student(1, "A", "M", 150);
        Student b = new Student(2, "B", "G", 160);
        Student c = new Student(3, "C", "M", 170);
        Student d = new Student(4, "D", "G", 180);
        Student e = new Student(5, "E", "M", 190);

        List<Student> students = new ArrayList<>();
        students.add(a);
        students.add(b);
        students.add(c);
        students.add(d);
        students.add(e);

        Iterator<Student> it = students.iterator();

        while (it.hasNext()) {
            Student student = it.next();
            //过滤性别为"G"的student
            if (student.getSex().equals("G")) {
                System.out.println(student.toString());
            }
        }

        System.out.println("======");
        students.stream()
                .filter(p -> p.getSex().equals("G"))
                .forEach(p -> System.out.println(p.toString()));

        //removeIf移除元素
        System.out.println("======");
        System.out.println("removeIf");
        students.removeIf(p -> "E".equals(p.getName()));
        students.stream().forEach(p -> System.out.println(p.toString()));

        System.out.println("======");
        Stream.iterate(1, item -> item + 1)
                .limit(10)
                .forEach(System.out::print);

        //concat可将两个Stream连接在一起
        System.out.println();
        System.out.println("======");
        Stream.concat(Stream.of(1, 2, 3), Stream.of(4, 5)).forEach(integer -> System.out.print(integer + " "));

        //distinct(去重)
        System.out.println();
        System.out.println("======");
        Stream.of(1, 2, 3, 1, 2, 3, 4).distinct().forEach(System.out::print);

        //filter过滤
        System.out.println();
        System.out.println("======");
        Stream.of(1, 2, 3, 4, 5).filter(item -> item > 3).forEach(System.out::print);

        //map,用于对Stream中的元素进行函数转换操作
        System.out.println();
        System.out.println("======");
        Stream.of("a", "b", "hello").map(p -> p.toUpperCase()).forEach(System.out::print);

        //flatMap用法(转换函数的对象是一个Stream)
        System.out.println();
        System.out.println("======");
        Stream.of(1, 2, 3).flatMap(p -> Stream.of(p * 10)).forEach(System.out::print);


        //peek,生成一个包含原Stream的所有元素的新Stream,同时会提供一个消费函数
        System.out.println();
        System.out.println("======");
        Stream.of(1, 2, 3, 4, 5).peek(p -> System.out.print("accept" + p)).forEach(System.out::print);

        //skip,过滤掉原Stream中的前N个元素
        System.out.println();
        System.out.println("======");
        Stream.of(1, 2, 3, 4, 5).skip(2).forEach(System.out::print);

        //sorted,用于排序(默认升序)
        System.out.println();
        System.out.println("======");
        Stream.of(5,4,3,2,1).sorted().forEach(System.out::print);
        System.out.println();
        Stream.of(1,2,3,4,5).sorted().forEach(System.out::print);

        //count,计数
        System.out.println();
        System.out.println("======");
        long count = Stream.of(1,2,3,4,5).count();
        System.out.println("count:" + count);

        //forEach,用于遍历Stream中的元素
        System.out.println("======");
        Stream.of(5,4,3,2,1).forEach(System.out::print);

        //forEachOrdered
        System.out.println();
        System.out.println("======");
        Stream.of(5,2,1,4,3).forEachOrdered(p -> System.out.print(p));

        //max与min
        System.out.println();
        System.out.println("======");
        Optional<Integer> min = Stream.of(1,2,3,4,5).max(((o1, o2) -> o2 - o1));
        System.out.println("min:" + min.get());

        System.out.println("======");
        Optional<Integer> max = Stream.of(1,2,3,4,5).max((Comparator.comparingInt(o -> o)));
        System.out.println("max:" + max.get());

        //allMatch,是否全部匹配
        boolean allMatch = Stream.of(1,2,3,4,5).allMatch(p -> p > 0);
        System.out.println("allMatch:" + allMatch);

        //anyMatch,是否有部分匹配
        boolean anyMatch = Stream.of(1,2,3,4,5).anyMatch(p -> p > 4);
        System.out.println("anyMatch:" + anyMatch);

        //findAny,用于取值
        Optional<Integer> any = Stream.of(1,2,3,4,5).findAny();
        System.out.println("any:" + any.get());

        //findFirst
        Optional<Integer> findFirst = Stream.of(1,2,3,4,5).findFirst();
        System.out.println("findFirst:" + findFirst.get());

        //limit
        Stream.of(1,2,3,4,5).limit(2).forEach(System.out::print);

        //noneMatch,是否全不匹配
        System.out.println();
        System.out.println("======");
        boolean noneMatch = Stream.of(1,2,3,4,5).noneMatch(p -> p > 10);
        System.out.println("noneMatch:" + noneMatch);

    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值