记录Java8流处理
/*
*author:cst
*如有错误请指出
*/
FlatMap()
Stream.flatMap(Function<? super String, ? extends Stream<? extends String>> mapper)
api:
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
返回一个流,原先流的每个原始被指定函数返回的流里所有元素替换。每一个返回的新流在替换进原先流的元素之后将关闭。(如果返回的流是null,那个将会使用一个空流来替代)
如:
List<Integer> list = Arrays.asList(3,5,6,9,23,69,6,3,8,63);
Stream<Integer> s = list.stream().flatMap(e->filteInteger(e));
s.forEach(e->System.out.println(e));
private static Stream<Integer> filteInteger(Integer i)
{
List<Integer> l = new LinkedList<Integer>();
if(i.intValue()>10)
{
l.add(1);l.add(2);
}
return l.stream();
}
//将会输出
1
2
1
2
1
2