这篇文章主要介绍了java8中的Stream常用方法有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
将现有数据结构转化成Stream
Stream<Integer> s = Stream.of(1, 2, 3, 4, 5);
Stream<Integer> s = Arrays.stream(arr);
Stream<Integer> s = aList.stream();
通过Stream.generate()方法:
// 这种方法通常表示无限序列
Stream<T> s = Stream.generate(SuppLier<T> s);
// 创建全体自然数的Stream
class NatualSupplier implements Supplier<BigInteger> {
BigInteger next = BigInteger.ZERO;
@Override
public BigInteger get() {
next = next.add(BigInteger.ONE);
return next;
}
}
通过其他方法返回
Stream<String> lines = Files.lines(Path.get(filename))
...
把一种操作运算映射到Stream的每一个元素上,从而完成一个Stream到另一个Stream的转换
map方法接受的对象是Function接口,这个接口是一个函数式接口:
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
@FunctionalInterface
public interface Function<T, R> {
// 将T转换为R
R apply(T t);
}
使用:
// 获取Stream里每个数的平方的集合
Stream<Integer> ns = Stream.of(1, 2, 3, 4, 5);
ns.map(n -> n * n).forEach(System.out::println);
map方法是一个一对一的映射,每输入一个数据也只会输出一个值。
flatMap方法是一对多的映射,对每一个元素映射出来的仍旧是一个Stream,然后会将这个子Stream的元素映射到父集合中:
Stream<List<Integer>> inputStream = Stream.of(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6));
List<Integer> integerList = inputStream.flatMap((childList) -> childList.stream()).collect(Collectors.toList());
//将一个“二维数组”flat为“一维数组”
integerList.forEach(System.out::println);
filter方法用于过滤Stream中的元素,并用符合条件的元素生成一个新的Stream。
filter方法接受的参数是Predicate接口对象,这个接口是一个函数式接口:
Stream<T> filter(Predicate<? super T>) predicate;
@FunctionInterface
public interface Predicate<T> {
// 判断元素是否符合条件
boolean test(T t);
}
使用
// 获取当前Stream所有偶数的序列
Stream<Integer> ns = Stream.of(1, 2, 3, 4, 5);
ns.filter(n -> n % 2 == 0).forEach(System.out::println);
limit用于限制获取多少个结果,与数据库中的limit作用类似,skip用于排除前多少个结果。
sorted函数需要传入一个实现Comparator函数式接口的对象,该接口的抽象方法compare接收两个参数并返回一个整型值,作用就是排序,与其他常见排序方法一致。
distinct用于剔除重复,与数据库中的distinct用法一致。
findFirst方法总是返回第一个元素,如果没有则返回空,它的返回值类型是Optional<T>类型,接触过swift的同学应该知道,这是一个可选类型,如果有第一个元素则Optional类型中保存的有值,如果没有第一个元素则该类型为空。
Stream<User> stream = users.stream();
Optional<String> userID = stream.filter(User::isVip).sorted((t1, t2) -> t2.getBalance() - t1.getBalance()).limit(3).map(User::getUserID).findFirst();
userID.ifPresent(uid -> System.out.println("Exists"));
min可以对整型流求最小值,返回OptionalInt。
max可以对整型流求最大值,返回OptionalInt。
这两个方法是结束操作,只能调用一次。
allMatch:Stream中全部元素符合传入的predicate返回 true
anyMatch:Stream中只要有一个元素符合传入的predicate返回 true
noneMatch:Stream中没有一个元素符合传入的predicate返回 true
reduce方法将一个Stream的每一个元素一次作用于BiFunction,并将结果合并。
reduce方法接受的方法是BinaryOperator接口对象。
Optional<T> reduce(BinaryOperator<T> accumulator);
@FuncationalInterface
public interface BinaryOperator<T> extends BiFunction<T, T, T> {
// Bi操作,两个输入,一个输出
T apply(T t, T u);
}
使用:
// 求当前Stream累乘的结果
Stream<Integer> ns = Stream.of(1, 2, 3, 4, 5);
int r = ns.reduce( (x, y) -> x * y ).get();
System.out.println(r);
感谢你能够认真阅读完这篇文章,希望小编分享的“java8中的Stream常用方法有哪些”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。