Collection.stream()是java1.8增加的新功能,功能强大,可以大大简便我们对集合写法。
一开始觉得这个东西有些复杂,各种转换让人摸不着头脑,但是实际上,那是因为没有记住每个方法的作用,每个方法都好比一个具有固定功能的积木,把不同类型的积木组合,就能产生我们想要的效果。
所以,文本就是一篇stream教学文档,希望读者阅读之后,能够熟练掌握stream的各种不同用法。
首先,设置两个对象类,如下,方面后面的讲解:
ListNode类
- public class ListNode {
- public int val;
- public ListNode next = null;
-
- public ListNode(int val) {
- this.val = val;
- }
-
- public ListNode(int val, ListNode next) {
- this.val = val;
- this.next = next;
- }
- }
Node类
- public class Node {
- public int val;
- public List
children = new ArrayList<>(); -
- public Node() {
- }
-
- public Node(int _val) {
- val = _val;
- }
-
- public Node(int _val, List
_children ) { - val = _val;
- children = _children;
- }
- }
- Collection<ListNode> nodeList = new ArrayList<>();
- nodeList.add(new ListNode(3));
- nodeList.add(new ListNode(4));
- nodeList.add(new ListNode(5));
-
- /**
- * 对集合进行删除操作
- */
- //当前集合中删除value==2的值
- nodeList.removeIf(o -> o.val == 2);
用法一和二不同点,是用法一是最当前集合进行操作,而用法二是把nodeList的集合进行一次的遍历,进行一些操作,然后添加到新的集合中。
- /**
- * 对集合进行筛选操作
- */
- //原来的集合中挑出来value==2的对象加入到新的集合中
- List<ListNode> collect1 = nodeList.stream().filter(listNode -> listNode.val == 2).collect(Collectors.toList());
-
我们来解释下这些代码的操作,这样理解会更容易一些。
首先,nodeList.stream()类似于把集合转换为一个有序流,
filter是针对每一个节点进行操作,返回值是boolean类型,true就代表符合,false就代表不符合,如果不用lamda表达式写,就是下面这种形式:
- nodeList.stream().filter(new Predicate<ListNode>() {
- @Override
- public boolean test(ListNode listNode) {
- return listNode.val == 2;
- }
- });
最后,collect方法就是收集。针对每个符合条件的的节点,都会被添加到这个新的集合当中。
那么我们把这个需求升级一下,能不能对List
- List<Node> collect5 = nodeList.stream().filter(listNode -> listNode.val == 2).map(new Function<ListNode, Node>() {
- @Override
- public Node apply(ListNode listNode) {
- return new Node(listNode.val);
- }
- }).collect(Collectors.toList());
可以看到,只是多了一步map的操作。map的操作就是进行一个类型转换,把ListNode转换为Node,apply方法中输入ListNode类型对象,我们只要根据我们实际需求,返回Node类型对象即可。
- //List转换为Set
- Set<ListNode> collect6 = nodeList.stream().collect(Collectors.toSet());
- //集合转换为数组
- ListNode[] listNodes = nodeList.toArray(new ListNode[0]);
- //集合转换为map
- Map<Integer, ListNode> collect = nodeList.stream().collect(Collectors.toMap(listNode -> listNode.val, listNode -> listNode));
这个之前我是最头疼的,int这种基础类型,是没有办法直接通过Arrays.asList方法进行直接转换的,必须要遍历,装箱操作把int转换为Integer,然后再加入到数组中,而使用strem就方便的多。
- //int数组转List<Integer>
- int[] ints = {1, 2, 3};
- List<Integer> collect2 = Arrays.stream(ints).boxed().collect(Collectors.toList());
我们可以看到,多了一步操作,boxed,装箱操作。
- int[] ints1 = collect2.stream().mapToInt(new ToIntFunction
() { - @Override
- public int applyAsInt(Integer value) {
- return value;
- }
- }).toArray();
- /**
- * 转换为long类型的集合
- */
- List<Long> collect3 = Arrays.stream(ints).asLongStream().boxed().collect(Collectors.toList());
- List<List<Integer>> lists = new ArrayList<>();
- lists.add(collect2);
- List<Integer> collect4 = lists.stream().flatMap(new Function<List<Integer>, Stream<Integer>>() {
- @Override
- public Stream<Integer> apply(List<Integer> integers) {
- return integers.stream();
- }
- }).collect(Collectors.toList());
1.如果在安卓上使用,minVersion>=24才可以。
2.如果还有什么新的用法需求,欢迎留言提问。