• java中Collection.stream()流用法详解


    前言:

    Collection.stream()是java1.8增加的新功能,功能强大,可以大大简便我们对集合写法。

    一开始觉得这个东西有些复杂,各种转换让人摸不着头脑,但是实际上,那是因为没有记住每个方法的作用,每个方法都好比一个具有固定功能的积木,把不同类型的积木组合,就能产生我们想要的效果。

    所以,文本就是一篇stream教学文档,希望读者阅读之后,能够熟练掌握stream的各种不同用法。

    首先,设置两个对象类,如下,方面后面的讲解:

    ListNode类

    1. public class ListNode {
    2. public int val;
    3. public ListNode next = null;
    4. public ListNode(int val) {
    5. this.val = val;
    6. }
    7. public ListNode(int val, ListNode next) {
    8. this.val = val;
    9. this.next = next;
    10. }
    11. }

    Node类

    1. public class Node {
    2. public int val;
    3. public List children = new ArrayList<>();
    4. public Node() {
    5. }
    6. public Node(int _val) {
    7. val = _val;
    8. }
    9. public Node(int _val, List _children) {
    10. val = _val;
    11. children = _children;
    12. }
    13. }

    用法一:对集合进行删减操作

    1. Collection<ListNode> nodeList = new ArrayList<>();
    2. nodeList.add(new ListNode(3));
    3. nodeList.add(new ListNode(4));
    4. nodeList.add(new ListNode(5));
    5. /**
    6. * 对集合进行删除操作
    7. */
    8. //当前集合中删除value==2的值
    9. nodeList.removeIf(o -> o.val == 2);

    用法二:对集合进行筛选生成新的集合

    2.1 对nodeList进行筛选,符合条件的加入到新集合当中

    用法一和二不同点,是用法一是最当前集合进行操作,而用法二是把nodeList的集合进行一次的遍历,进行一些操作,然后添加到新的集合中。

    1. /**
    2. * 对集合进行筛选操作
    3. */
    4. //原来的集合中挑出来value==2的对象加入到新的集合中
    5. List<ListNode> collect1 = nodeList.stream().filter(listNode -> listNode.val == 2).collect(Collectors.toList());

    我们来解释下这些代码的操作,这样理解会更容易一些。

    首先,nodeList.stream()类似于把集合转换为一个有序流,

    filter是针对每一个节点进行操作,返回值是boolean类型,true就代表符合,false就代表不符合,如果不用lamda表达式写,就是下面这种形式:

    1. nodeList.stream().filter(new Predicate<ListNode>() {
    2. @Override
    3. public boolean test(ListNode listNode) {
    4. return listNode.val == 2;
    5. }
    6. });

    最后,collect方法就是收集。针对每个符合条件的的节点,都会被添加到这个新的集合当中。

    2.2 对List进行筛选,符合条件的转换为Node类型到新集合当中

    那么我们把这个需求升级一下,能不能对List的集合进行筛选,然后生成List类型的集合呢?这在实际场景中还是很普遍的,当然可以,stream几乎可以满足我们一切的需求,我们只需要在原来的基础上加一步转换集合。

    1. List<Node> collect5 = nodeList.stream().filter(listNode -> listNode.val == 2).map(new Function<ListNode, Node>() {
    2. @Override
    3. public Node apply(ListNode listNode) {
    4. return new Node(listNode.val);
    5. }
    6. }).collect(Collectors.toList());

    可以看到,只是多了一步map的操作。map的操作就是进行一个类型转换,把ListNode转换为Node,apply方法中输入ListNode类型对象,我们只要根据我们实际需求,返回Node类型对象即可。

    用法三:集合类型转换

    3.1 List转换为Set类型

    1. //List转换为Set
    2. Set<ListNode> collect6 = nodeList.stream().collect(Collectors.toSet());

    3.2 List转换为数组类型

    1. //集合转换为数组
    2. ListNode[] listNodes = nodeList.toArray(new ListNode[0]);

    3.3 List转换为Map类型

    1. //集合转换为map
    2. Map<Integer, ListNode> collect = nodeList.stream().collect(Collectors.toMap(listNode -> listNode.val, listNode -> listNode));

    用法四:拆装箱操作

    4.1 int[]转List

    这个之前我是最头疼的,int这种基础类型,是没有办法直接通过Arrays.asList方法进行直接转换的,必须要遍历,装箱操作把int转换为Integer,然后再加入到数组中,而使用strem就方便的多。

    1. //int数组转List<Integer>
    2. int[] ints = {1, 2, 3};
    3. List<Integer> collect2 = Arrays.stream(ints).boxed().collect(Collectors.toList());

    我们可以看到,多了一步操作,boxed,装箱操作。

    4.2 List转换为int[]

    1. int[] ints1 = collect2.stream().mapToInt(new ToIntFunction() {
    2. @Override
    3. public int applyAsInt(Integer value) {
    4. return value;
    5. }
    6. }).toArray();

    4.2 int[]转换为List

    1. /**
    2. * 转换为long类型的集合
    3. */
    4. List<Long> collect3 = Arrays.stream(ints).asLongStream().boxed().collect(Collectors.toList());

    用法五:双重集合转换

    5.1双重集合转单层

    1. List<List<Integer>> lists = new ArrayList<>();
    2. lists.add(collect2);
    3. List<Integer> collect4 = lists.stream().flatMap(new Function<List<Integer>, Stream<Integer>>() {
    4. @Override
    5. public Stream<Integer> apply(List<Integer> integers) {
    6. return integers.stream();
    7. }
    8. }).collect(Collectors.toList());

    备注:

    1.如果在安卓上使用,minVersion>=24才可以。

    2.如果还有什么新的用法需求,欢迎留言提问。

  • 相关阅读:
    堆--数据流中第K大元素
    神经网络(六)循环神经网络
    ISO三体系认证有哪些意义和好处?
    Ipad2022可以用电容笔吗?双十一值得入手电容笔推荐
    linux篇【9】:进程间通信——<前序>
    安装RabbitMQ出现Plugin configuration unchanged.问题
    Oracle中国C/C++软件工程师代码编写规范2017公开版
    pandas 筛选数据的 8 个骚操作
    美的、格力逆境求生
    免费翻译软件-批量自动一键翻译
  • 原文地址:https://blog.csdn.net/AA5279AA/article/details/127773092