forEach遍历

/*
forEach:该办法接管一个Consumer接口函数,将每一个流元素交给该函数解决
简略记:
forEach办法:用来遍历流中的数据
是一个终结办法,遍历之后就不能持续调用Stream流中的其余办法
*/

public class demo01Stream_ForEach {public static void main(String[] args) {    //获取一个Stream流Stream<String>stream= Stream.of("张三","李四","王五","赵六");//应用Stream流的办法forEach对stream流中的数据遍历    stream.forEach((String name)->{        System.out.println(name);    });}}

filter过滤

/*
filter:用于对Stream流中的数据进行过滤
filter(Predicate<? super T>predicate)
filter办法的参数Predicate是一个函数式接口,能够应用lambda表达式
Predicate中的形象办法
boolean test(T t)
*/

public class demo01Stream_filter {public static void main(String[] args) {    //创立一个Stream流    Stream<String> stream = Stream.of("张三", "李四", "王五", "赵六", "冯老七");    Stream<String> stream1 = stream.filter((String name) -> {        return name.startsWith("冯");    });    //遍历stream1    stream1.forEach((name)-> System.out.println(name));}}

*Stream流属于管道流,只能被生产一次
第一个Stream流调用结束办法,数据会流转到下一个流中
第一个流会敞开,不再被调用*

map办法转换流

/*
将流中的元素映射到另一个流中,应用map办法
该接口须要一个Function函数式接口参数,能够将以后流中的T类型数据转换为另一种R类型的流
Function中的形象办法:
R apply(T t)
*/

public class demoStream_map {public static void main(String[] args) {    //获取一个String类型的stream流    Stream<String>stream = Stream.of("1","2","3","4");    //应用map办法,把字符串类型的整数,转换(映射)为Integer类型的整数    Stream<Integer> stream1 = stream.map((String s) -> {        return Integer.parseInt(s);    });    stream1.forEach((i)-> System.out.println(i));}}

count办法 统计个数

/*
count办法 用于统计Stream流中的元素个数
long count();
count办法是一个终结办法,返回值是一个long类型的整数
不能再调用Stream流其余办法
*/

public class demoStream_count {public static void main(String[] args) {    ArrayList<Integer> list= new ArrayList<>();    list.add(1);    list.add(2);    list.add(3);    list.add(4);    list.add(5);    Stream<Integer> stream = list.stream();    long count = stream.count();    System.out.println(count);}}

limit截取元素

/*
limit:用于截取流中的元素
limit能够对流进行截取,只取用前n个
limit(long maxSize);
参数是一个long型,如果汇合以后长度大于参数则进行截取,否则不进行操作
limit是一个提早办法,能够持续应用Stream流办法
*/

public class demoStream_limit {public static void main(String[] args) {    String[] arr= {"xxx","wwq","wqew","wewqewqe"};    Stream<String> stream = Stream.of(arr);    Stream<String> stream1 = stream.limit(2);    stream1.forEach((name)-> System.out.println(name));}}

skip办法跳过元素

/*
skip办法:用于跳过元素
skip(long n)
如果流的以后长度大于n,则跳过前n个,否则将会失去一个长度为0的空流
*/

public class demoStream_skip {public static void main(String[] args) {    String[] arr= {"xxx","wwq","wqew","wewqewqe"};    Stream<String> stream = Stream.of(arr);    Stream<String> stream1 = stream.skip(2);    stream1.forEach((name)-> System.out.println(name));}}

concat办法合并流

/*
concat:用于把流组合到一起
如果有两个流,心愿合并成为一个流,能够应用Stream接口的静态方法concat

*/

public class demoStream_concat {public static void main(String[] args) {    Stream<String> stream = Stream.of("张三", "李四", "王五", "赵六", "冯老七");    String[] arr= {"QWQ", "ERE", "TYT", "UIU", "OIO"};    Stream<String> stream1 = Stream.of(arr);    //合并    Stream<String> stream2 = Stream.concat(stream, stream1);    stream2.forEach((name)-> System.out.print(name+" "));}}

最初

欢送关注公众号:前程有光,支付一线大厂Java面试题总结+各知识点学习思维导+一份300页pdf文档的Java外围知识点总结!