Java Stream API 操作完全攻略:让你的代码更加出色 (三)

1,573 阅读5分钟

pexels-luis-del-río-15286.jpg

前言

  Java Stream 是一种强大的数据处理工具,可以帮助开发人员快速高效地处理和转换数据流。使用 Stream 操作可以大大简化代码,使其更具可读性和可维护性,从而提高开发效率。本文将为您介绍 Java Stream 操作的所有方面,包括 groupingBy、groupingBy、joining、mapping 等操作,让你的代码行云流水,更加优雅。

  1. groupingBy():按照指定条件对 Stream 中的元素进行分组。
  2. partitioningBy():按照指定条件对 Stream 中的元素进行分区。
  3. joining():将 Stream 中的元素连接成一个字符串。
  4. mapping():根据指定的 Function 对 Stream 中的元素进行映射,并返回一个新的 Stream。
  5. flatMapping():将每个元素映射为一个 Stream,然后将这些 Stream 连接成一个 Stream。
  6. iterating():使用指定的种子值创建一个 Stream,并依次对每个元素进行指定的操作。
  7. empty():返回一个空的 Stream。
  8. of():根据指定的元素创建一个 Stream。
  9. concat():将多个 Stream 连接成一个 Stream。
  10. unordered():返回一个无序的 Stream。

示例

1. 使用 groupingBy() 按照字符串长度对字符串列表进行分组

代码示例:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GroupingByExample {
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("hello", "world", "java", "stream");

        Map<Integer, List<String>> result = stringList.stream()
                .collect(Collectors.groupingBy(String::length));

        System.out.println(result);
    }
}

输出结果:

{5=[hello, world], 4=[java], 6=[stream]}

2.使用 partitioningBy() 将数字列表按照是否为偶数进行分区

代码示例:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PartitioningByExample {
    public static void main(String[] args) {
        List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        Map<Boolean, List<Integer>> result = numberList.stream()
                .collect(Collectors.partitioningBy(n -> n % 2 == 0));

        System.out.println(result);
    }
}

输出结果:

{false=[1, 3, 5, 7, 9], true=[2, 4, 6, 8, 10]}

3.使用 joining() 将字符串列表中的元素用逗号连接成一个字符串

代码示例:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class JoiningByExample {
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("hello", "world", "java", "stream");

        String result = stringList.stream()
                .collect(Collectors.joining(","));

        System.out.println(result);
    }
}

输出结果:

hello,world,java,stream

4.使用 mapping() 将字符串列表中的元素转换为大写字母

代码示例:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MappingByExample {
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("hello", "world", "java", "stream");

        List<String> result = stringList.stream()
                .map(String::toUpperCase)
                .collect(Collectors.toList());

        System.out.println(result);
    }
}

输出结果:

[HELLO, WORLD, JAVA, STREAM]

5.使用 flatMapping() 将嵌套的字符串列表展平为一个字符串列表

代码示例:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FlatMappingByExample {
    public static void main(String[] args) {
        List<List<String>> nestedList = Arrays.asList(
                Arrays.asList("hello", "world"),
                Arrays.asList("hello","java", "stream"));

        List<String> result = nestedList.stream()
                .flatMap(List::stream)
                .distinct()
                .collect(Collectors.toList());

        System.out.println(result);
    }
}

输出结果:

[hello, world, java, stream]

6.使用 iterating() 生成斐波那契数列前 10 项

代码示例:

import java.util.stream.Stream;

public class IteratingByExample {
    public static void main(String[] args) {
        Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]})
                .limit(10)
                .map(t -> t[0])
                .forEach(System.out::println);
    }
}

输出结果:

0
1
1
2
3
5
8
13
21
34

7.使用 empty() 使用空的 Stream

代码示例:

import java.util.stream.Stream;

public class EmptyStreamExample {
    public static void main(String[] args) {
        Stream<String> emptyStream = Stream.empty();

        System.out.println(emptyStream.count());
    }
}

输出结果:

0

8.使用 of() 创建包含一些字符串的 Stream

代码示例:

import java.util.stream.Stream;

public class OfStreamExample {
    public static void main(String[] args) {
        Stream<String> stringStream = Stream.of("hello", "world", "java", "stream");

        stringStream.forEach(System.out::println);
    }
}

输出结果:

hello
world
java
stream

9.使用 concat() 将两个字符串列表合并为一个列表

代码示例:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class ConcatStreamExample {
    public static void main(String[] args) {
        List<String> list1 = Arrays.asList("hello", "world");
        List<String> list2 = Arrays.asList("java", "stream");

        Stream<String> stream1 = list1.stream();
        Stream<String> stream2 = list2.stream();

        List<String> result = Stream.concat(stream1, stream2)
                .collect(Collectors.toList());

        System.out.println(result);
    }
}

输出结果:

[hello, world, java, stream]

10.使用 unordered() 对数字列表进行排序后,使用 unordered()返回一个无序的 Stream

代码示例:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class UnorderedStreamExample {
    public static void main(String[] args) {
        List<Integer> numberList = Arrays.asList(1, 3, 2, 4, 10, 6, 8, 7, 9, 6);

        List<Integer> result = numberList.stream()
                .sorted()
                .unordered()
                .collect(Collectors.toList());

        System.out.println(result);
    }
}

输出结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

来个小坑

  感谢大佬看到这里,如果你看完觉得没问题,那么你需要反思一下了哦。好了我们可以在仔细地看看,示例10.unordered()的代码。

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class UnorderedStreamExample {
    public static void main(String[] args) {
        List<Integer> numberList = Arrays.asList(1, 3, 2, 4, 10, 6, 8, 7, 9, 6);

        List<Integer> result = numberList.stream()
                .sorted()
                .unordered()
                .collect(Collectors.toList());

        System.out.println(result);
    }
}

输出结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

  执行过程分析,首先输入了一个无序的流,然后使用sorted()对流进行排序,然后在使用unordered(),去除流的有序约束。然后输出为List,再进行打印出来。按理来说,输出的顺序应该是一个无序的,而不是有序的。

解答

  unordered()操作不会执行任何操作来显式地对流进行排序。它的作用是消除了流必须保持有序的约束,从而允许后续操作使用不必考虑排序的优化。

  对于顺序流,顺序的存在与否不会影响性能,只影响确定性。如果流是顺序的,则在相同的源上重复执行相同的流管道将产生相同的结果;如果是非顺序流,重复执行可能会产生不同的结果。

  unordered()操作,是消除了流必须保持有序的约束。并不会改变,流原有的顺序。对于并行流,放宽排序约束有时可以实现更高效的执行。在流有序时, 但用户不特别关心该顺序的情况下,使用 unordered() 明确地对流进行去除有序约束可以改善某些有状态或终端操作的并行性能。

参考

结尾

  如果觉得对你有帮助,可以多多评论,多多点赞哦,也可以到我的主页看看,说不定有你喜欢的文章,也可以随手点个关注哦,谢谢。

  我是不一样的科技宅,每天进步一点点,体验不一样的生活。我们下期见!