Java8新特性-Stream流

参考博客
https://www.cnblogs.com/CarpenterLee/p/6545321.html https://www.cnblogs.com/CarpenterLee/p/6550212.html
https://zhuanlan.zhihu.com/p/92976229
前两篇是很仔细的原理和用法,很好!

  • 写在最前面:
    • stream并不是某种数据结构,它只是数据源的一种视图。这里的数据源可以是一个数组,Java容器或I/O channel等。正因如此要得到一个stream通常不会手动创建,而是调用对应的工具方法,比如:
      • 调用Collection.stream()或者Collection.parallelStream()方法
      • 调用Arrays.stream(T[] array)方法
1.Filter
//函数原型为Stream<T> filter(Predicate<? super T> predicate)
//作用是过滤出满足predicate条件的元素
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
long count = strings.stream().filter(string -> string.isEmpty()).count(); // 2

调用filter()Stream流中元素就剩下了符合要求的元素,可以继续对这些元素进行操作。

2.Sorted
//函数原型为Stream<T> sorted(Comparator<? super T> comparator)排序函数有两个,一个是用自然顺序排序,一个是使用自定义比较器排序,函数原型分别为Stream<T> sorted()和Stream<T> sorted(Comparator<? super T> comparator)
//作用是对列表中的元素排序。
List<String> myList = Arrays.asList("1", "2", "3");
myList.stream()
        .sorted(Comparator.comparingInt(Integer::parseInt))
        .forEach(System.out::println);
list.forEach(System.out::println);
//这行代码就是下行代码
list.forEach(x->{
    System.out.println(x);
});
//1.foreach支持函数式编程。
//2.println是System.out的一个方法,所以可以使用此种方式
3.map
函数原型为<R> Stream<R> map(Function<? super T,? extends R> mapper),作用是返回一个对当前所有元素执行执行mapper之后的结果组成的Stream。直观的说,就是对每个元素按照某种操作进行转换,转换前后Stream中元素的个数不会改变,但元素的类型取决于转换之后的类型。
//可以通过map拿到容器中对象的某个属性e->e::a
Stream<String> stream = Stream.of("I", "love", "you", "too");
stream.map(str -> str.toUpperCase())
    .forEach(str -> System.out.println(str));
4. ForEach
函数原型为void forEach(Consumer<? super T> action)
作用是对每一个元素的执行action操作
List<String> myList = Arrays.asList("1", "2", "3");
myList.stream()
    .forEach(System.out::println);
5.Collector
collect 方法接收一个 Collector,Collector 可以将 Stream 转换成集合结果,比如 List Set Map。JDK内置了常用的 Collector,所以大多数情况下我们不需要自己实现.
List<String> list3 = Arrays.asList("a1", "b2", "c3");
List<String> a = list3.stream()
    .filter(s -> !s.startsWith("a"))
    .collect(Collectors.toList());

String a1 = list3.stream()
    .filter(s -> !s.startsWith("a"))
    .collect(Collectors.joining(","));
// 将Stream转换成List或Set
Stream<String> stream = Stream.of("I", "love", "you", "too");
List<String> list = stream.collect(Collectors.toList()); // (1)
Set<String> set = stream.collect(Collectors.toSet()); // (2)
//上述代码能够满足大部分需求,但由于返回结果是接口类型,我们并不知道类库实际选择的容器类型是什么,有时候我们可能会想要人为指定容器的实际类型,这个需求可通过Collectors.toCollection(Supplier<C> collectionFactory)方法完成。
// 使用toCollection()指定规约容器的类型
ArrayList<String> arrayList = stream.collect(Collectors.toCollection(ArrayList::new));// (3)
HashSet<String> hashSet = stream.collect(Collectors.toCollection(HashSet::new));// (4)
6.distinct()
函数原型为Stream<T> distinct(),作用是返回一个去除重复元素之后的Stream。
Stream<String> stream= Stream.of("I", "love", "you", "too", "too");
stream.distinct()
    .forEach(str -> System.out.println(str));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值