Stream接口:
- 实现类
- IntStream
- DoubleStream
- LongStream
一、创建Stream
- 1、方式一:通过集合创建
集合对象.stream() - 2、方式二:通过数组工具类Arrays
Arrays.stream(数组对象) - 3、方式三:Stream接口的静态方法of方法,产生一个有限流
Stream.of(…) - 4、方式四:Stream接口的静态方法
- (1)generate方法,产生无限流
- (2)Stream iterate(T seed, UnaryOperator f)
public class Test07StreamCreate {
@Test
public void test06(){
/*
* Stream<T> iterate(T seed, UnaryOperator<T> f)
* UnaryOperator接口,SAM接口,抽象方法:
*
* UnaryOperator<T> extends Function<T,T>
* T apply(T t)
*/
Stream<Integer> stream = Stream.iterate(1, num -> num+=2);
// stream = stream.limit(10);
stream.forEach(System.out::println);
}
@Test
public void test05(){
Stream<Double> stream = Stream.generate(Math::random);
stream.forEach(System.out::println);
}
@Test
public void test04(){
Stream<Integer> stream = Stream.of(1,2,3,4,5);
stream.forEach(System.out::println);
}
@Test
public void test03(){
String[] arr = {"hello","world"};
Stream<String> stream = Arrays.stream(arr);
}
@Test
public void test02(){
int[] arr = {1,2,3,4,5};
IntStream stream = Arrays.stream(arr);
}
@Test
public void test01(){
List<Integer> list = Arrays.asList(1,2,3,4,5);
//JDK1.8中,Collection系列集合增加了方法
Stream<Integer> stream = list.stream();
}
}
二、中间的加工操作
-
(1)filter(Predicate p):过滤
-
(2)distinct():去重
-
(3)limit(long maxSize):取有限的几个
-
(4)skip(long n):跳过n个
-
(5)peek(Consumer action) 接收Lambda表达式,对流中的每个数据执行Lambda体操作
-
(6)sorted():排序,按照自然排序
sorted(Comparator com):排序,按照定制排序 -
(7)map(Function f):接收Lambda表达式,对流中的每个数据,执行Lambda体操作,返回新的数据构成新的流
-
(8)flatMap(Function f)
map(Function<? super T,? extends R> mapper) map操作流中的把T对象变成R对象,由R对象构成新的流
flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
flatMap把流中的数据T对象压扁变成一个Stream,然后把一个个的Stream最后再合成一个大的Stream
public class Test08StreamMiddle {
@Test
public void test12(){
String[] arr = {"hello","world","java"};
Stream<String> flatMap = Arrays.stream(arr)
.flatMap(t -> Stream.of(t.split("|")));//Function<T,R>接口抽象方法 R apply(T t) 现在的R是一个Stream
flatMap.forEach(System.out::println);
}
@Test
public void test11(){
String[] arr = {"hello","world","java"};
Arrays.stream(arr)
.map(t->t.toUpperCase())
.forEach(System.out::println);
}
@Test
public void test10(){
Stream.of(1,2,3,4,5)
.map(t -> t+=1)//Function<T,R>接口抽象方法 R apply(T t)
.forEach(System.out::println);
}
@Test
public void test09(){
//希望能够找出前三个最大值,前三名最大的,不重复
Stream.of(11,2,39,4,54,6,2,22,3,3,4,54,54)
.distinct()
.sorted((t1,t2) -> -Integer.compare(t1, t2))//Comparator接口 int compare(T t1, T t2)
.limit(3)
.forEach(System.out::println);
}
@Test
public void test08(){
long count = Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
.distinct()
.peek(System.out::println) //Consumer接口的抽象方法 void accept(T t)
.count();
System.out.println("count="+count);
}
@Test
public void test07(){
Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
.skip(5)
.distinct()
.filter(t -> t%3==0)
.forEach(System.out::println);
}
@Test
public void test06(){
Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
.skip(5)
.forEach(System.out::println);
}
@Test
public void test05(){
Stream.of(1,2,2,3,3,4,4,5,2,3,4,5,6,7)
.distinct() //(1,2,3,4,5,6,7)
.filter(t -> t%2!=0) //(1,3,5,7)
.limit(3)
.forEach(System.out::println);
}
@Test
public void test04(){
Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
.limit(3)
.forEach(System.out::println);
}
@Test
public void test03(){
Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
.distinct()
.forEach(System.out::println);
}
@Test
public void test02(){
Stream.of(1,2,3,4,5,6)
.filter(t -> t%2==0)
.forEach(System.out::println);
}
@Test
public void test01(){
//1、创建Stream
Stream<Integer> stream = Stream.of(1,2,3,4,5,6);
//2、加工处理
//过滤:filter(Predicate p)
//把里面的偶数拿出来
/*
* filter(Predicate p)
* Predicate是函数式接口,抽象方法:boolean test(T t)
*/
stream = stream.filter(t -> t%2==0);
//3、终结操作:例如:遍历
stream.forEach(System.out::println);
}
}
三、终结操作
-
1、void forEach(Consumer ):遍历流中的数据
-
2、long count():统计个数
-
3、boolean allMatch(Predicate p):是否全部满足xx条件
boolean anyMatch(Predicate p):是否有一个满足xx条件
boolean noneMatch(Predicate p):是否全部都不满足xx条件 -
4、 Optional findFirst():返回第一个
Optional findAny():返回任意一个 -
5、Optional max(Comparator c):找出最大的
Optional min(Comparator c) :找出最小的 -
6、T reduce(T iden, BinaryOperator b) 通过反复的运算,留下最后一个结果
Optional reduce(BinaryOperator b) -
7、R collect(Collector c):把流中的数据最后都收集到一起
Collector接口BiConsumer 抽象方法 void accept(R r,T t)
public class Test09StreamEnding {
@Test
public void test14(){
List<Integer> list = Stream.of(1,2,4,5,7,8)
.filter(t -> t%2==0)
.collect(Collectors.toList());
System.out.println(list);
}
@Test
public void test13(){
Optional<Integer> reduce = Stream.of(1,2,4,5,7,8)
.reduce((t1,t2) -> t1>t2?t1:t2);//BinaryOperator接口 T apply(T t1, T t2)
System.out.println(reduce);
}
@Test
public void test12(){
Integer reduce = Stream.of(1,2,4,5,7,8)
.reduce(0, (t1,t2) -> t1+t2);//BinaryOperator接口 T apply(T t1, T t2)
System.out.println(reduce);
}
@Test
public void test11(){
Optional<Integer> max = Stream.of(1,2,4,5,7,8)
.max((t1,t2) -> Integer.compare(t1, t2));
System.out.println(max);
}
@Test
public void test10(){
Optional<Integer> opt = Stream.of(1,2,4,5,7,8)
.filter(t -> t%3==0)
.findFirst();
System.out.println(opt);
}
@Test
public void test09(){
Optional<Integer> opt = Stream.of(1,2,3,4,5,7,9)
.filter(t -> t%3==0)
.findFirst();
System.out.println(opt);
}
@Test
public void test08(){
Optional<Integer> opt = Stream.of(1,3,5,7,9).findFirst();
System.out.println(opt);
}
@Test
public void test04(){
boolean result = Stream.of(1,3,5,7,9)
.anyMatch(t -> t%2==0);
System.out.println(result);
}
@Test
public void test03(){
boolean result = Stream.of(1,3,5,7,9)
.allMatch(t -> t%2!=0);
System.out.println(result);
}
@Test
public void test02(){
long count = Stream.of(1,2,3,4,5)
.count();
System.out.println("count = " + count);
}
@Test
public void test01(){
Stream.of(1,2,3,4,5)
.forEach(System.out::println);
}
}