JAVA Stream流详细使用方法

发布时间:2024年01月17日

1.Stream的创建:

// 从集合创建Stream
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> streamFromList = numbers.stream();

// 从数组创建Stream
String[] words = {"Hello", "World", "Java"};
Stream<String> streamFromArray = Arrays.stream(words);

// 使用Stream.of()创建Stream
Stream<Integer> streamOfValues = Stream.of(1, 2, 3, 4, 5);

// 创建一个无限流
Stream<Integer> infiniteStream = Stream.iterate(0, n -> n + 1);

2.中间操作:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

// filter():筛选
Stream<Integer> filteredStream = numbers.stream()
                                        .filter(n -> n % 2 == 0);

// map():转换
Stream<String> mappedStream = numbers.stream()
                                    .map(n -> "Number: " + n);

// sorted():排序
Stream<Integer> sortedStream = numbers.stream()
                                      .sorted();

// distinct():去重
Stream<Integer> distinctStream = numbers.stream()
                                         .distinct();

// limit():限制元素个数
Stream<Integer> limitedStream = numbers.stream()
                                       .limit(3);

// skip():跳过元素
Stream<Integer> skippedStream = numbers.stream()
                                       .skip(2);

3. 最终操作:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

// forEach():遍历每个元素
numbers.stream()
       .forEach(System.out::println);

// toArray():转换为数组
Object[] numberArray = numbers.stream()
                             .toArray();

// collect():收集到集合或映射中
List<Integer> numberList = numbers.stream()
                                 .collect(Collectors.toList());

Set<Integer> numberSet = numbers.stream()
                               .collect(Collectors.toSet());

Map<Integer, String> numberMap = numbers.stream()
                               .collect(Collectors.toMap(n -> n, n -> "Number: " + n));

// reduce():聚合操作
Optional<Integer> sum = numbers.stream()
                               .reduce((a, b) -> a + b);

int max = numbers.stream()
                 .reduce(Integer.MIN_VALUE, Integer::max);

// anyMatch()、allMatch()、noneMatch():元素匹配判断
boolean anyMatch = numbers.stream()
                          .anyMatch(n -> n > 3);

boolean allMatch = numbers.stream()
                          .allMatch(n -> n > 0);

boolean noneMatch = numbers.stream()
                           .noneMatch(n -> n < 0);

// findFirst()、findAny():查找元素
Optional<Integer> first = numbers.stream()
                                .findFirst();

Optional<Integer> any = numbers.stream()
                              .findAny();

// count():元素数量
long count = numbers.stream()
                   .count();

?4.分组和分区:

List<Person> people = Arrays.asList(
        new Person("John", 30),
        new Person("Jane", 25),
        new Person("Mary", 30),
        new Person("Mark", 28),
        new Person("Alice", 25)
);

// 按年龄分组
Map<Integer, List<Person>> peopleByAge = people.stream()
                                               .collect(Collectors.groupingBy(Person::getAge));

// 按年龄分区
Map<Boolean, List<Person>> peopleByAgePartition = people.stream()
                                                       .collect(Collectors.partitioningBy(person -> person.getAge() > 25));

?5.并行流操作:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

// 使用并行流求和
int sum = numbers.parallelStream()
                 .reduce(0, Integer::sum);

// 使用并行流过滤条件
List<Integer> filteredNumbers = numbers.parallelStream()
                                       .filter(n -> n > 5)
                                       .collect(Collectors.toList());
文章来源:https://blog.csdn.net/vdssdf/article/details/135650620
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。