增强的Stream API
增强的Stream API1.概述2.具体方法//java9新特性十:Stream API的加强@Testpublic void test1(){List<Integer> list = Arrays.asList(23, 43, 45, 55, 61, 54, 32, 2, 45, 89, 7);//takeWhile 返回从开头开始的按照指定规则尽量多的元素//list.strea
·
增强的Stream API
1.概述
2.具体方法
//java9新特性十:Stream API的加强
@Test
public void test1(){
List<Integer> list = Arrays.asList(23, 43, 45, 55, 61, 54, 32, 2, 45, 89, 7);
//takeWhile 返回从开头开始的按照指定规则尽量多的元素
// list.stream().takeWhile(x -> x < 60).forEach(System.out::println);
//dropWhile():与 takeWhile 相反,返回剩余的元素。
list.stream().dropWhile(x -> x < 60).forEach(System.out::println);
}
@Test
public void test2(){
//of()参数中的多个元素,可以包含null值
Stream<Integer> stream1 = Stream.of(1, 2, 3,null);
stream1.forEach(System.out::println);
//of()参数不能存储单个null值。否则,报异常
// Stream<Object> stream2 = Stream.of(null);
// stream2.forEach(System.out::println);
Integer i = 10;
i = null;
//ofNullable():形参变量是可以为null值的单个元素
Stream<Integer> stream3 = Stream.ofNullable(i);
long count = stream3.count();
System.out.println(count);
}
@Test
public void test3(){
Stream.iterate(0,x -> x + 1).limit(10).forEach(System.out::println);
//java9中新增的重载的方法
Stream.iterate(0,x -> x < 100,x -> x + 1).forEach(System.out::println);
}
更多推荐
所有评论(0)