Summary of common methods of stream API in Java 8

What is stream?

Stream regards the set of elements to be processed as a stream. In the process of streaming, the stream API is used to operate the elements in the stream, such as filtering, sorting, aggregation, etc.

Streams can be created from arrays or collections. There are two kinds of operations for convection:

In addition, stream has several features:

Creation of stream

public class BuildStream {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1,2,3);
        // 通过集合的stream()方法创建顺序流
        Stream<Integer> stream1 = list.stream();

        // 通过集合的parallelStream()方法创建顺序流
        Stream<Integer> parallelStream1 = list.parallelStream();

        // 通过parallel()将顺序流转化为并行流
        Stream<Integer> parallelStream2 = list.stream().parallel();

        int[] arr = {1,3};
        // 通过数组创建流
        IntStream stream2 = Arrays.stream(arr);

        // 通过Stream的静态方法创建流
        Stream<Integer> stream3 = Stream.of(1,3);
        Stream<Integer> stream4 = Stream.iterate(1,x -> x + 1).limit(3);

        stream1.forEach(x -> System.out.print(x + " "));
        System.out.println(); // 1 2 3

        parallelStream1.forEach(x -> System.out.print(x + " "));
        System.out.println(); // 随机
    }
}

Test API

New test data

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Book {

    Long id;
    String title;
    String author;
    Integer pageCount;
    Double price;
}

public class TestStream {

    List<Book> bookList = new ArrayList<>();

    @Before
    public void init() {
        bookList.add(Book.builder().author("天乔巴夏").id(1L).title("Java-Spring").pageCount(100).price(50d).build());
        bookList.add(Book.builder().author("summerday").id(2L).title("Java-SpringBoot").pageCount(200).price(100d).build());
        bookList.add(Book.builder().author("hyh").id(3L).title("MysqL").pageCount(500).price(150d).build());
        bookList.add(Book.builder().author("tqbx").id(4L).title("Linux").pageCount(30).price(10d).build());
    }
}

findFirst、findAny

        // 匹配第一个
        Optional<Book> first = bookList.stream().filter(book -> book.getPageCount() > 100).findFirst();
        first.ifPresent(book -> System.out.println("匹配第一个值 : " + book));

        // 匹配任意
        Optional<Book> any = bookList.parallelStream().filter(book -> book.getPageCount() > 100).findAny();
        any.ifPresent(book -> System.out.println("匹配任意的值 : " + book));

anyMatch、noneMatch

        // 是否包含符合条件的书
        boolean anyMatch = bookList.stream().anyMatch(book -> book.getPageCount() > 100);
        System.out.println("是否存在页数大于100的书 : " + anyMatch);

        // 检查是否有名字长度大于5 的
        boolean noneMatch = bookList.stream().noneMatch(book -> (book.getTitle().length() > 5));
        System.out.println("不存在title长度大于5的书 : " + noneMatch);

filter

        // 找到所有id为奇数的书,列出他们的书名到list中
        List<String> titleList = bookList.stream()
                .filter(book -> book.getId() % 2 == 1)
                .map(Book::getTitle)
                .collect(Collectors.toList());
        System.out.println(titleList);

max、count

        // 获取页数最多的书
        Optional<Book> max = bookList.stream().max(Comparator.comparingInt(Book::getPageCount));
        max.ifPresent(book -> System.out.println("页数最多的书 : " + book));
        // 计算MysqL书籍有几本
        long count = bookList.stream().filter(book -> book.getTitle().contains("MysqL")).count();
        System.out.println("MysqL书籍的本数 : " + count);

peek、map

        // 将所有的书的价格调高100并输出调高以后的书单
        List<Book> result = bookList.stream().peek(book -> book.setPrice(book.getPrice() + 100))
                .collect(Collectors.toList());
        result.forEach(System.out::println);
        // 获取所有书的id列表
        List<Long> ids = bookList.stream().map(Book::getId).collect(Collectors.toList());
        System.out.println(ids);

reduce

        // 求所有书籍的页数之和
        Integer totalPageCount = bookList.stream().reduce(0,(s,book) -> s += book.getPageCount(),Integer::sum);
        System.out.println("所有书籍的页数之和 : " + totalPageCount);

collect

        // 将所有书籍存入 author -> title 的map中
        Map<String,String> map = bookList.stream().collect(Collectors.toMap(Book::getAuthor,Book::getTitle));
        // 取出所有id为偶数的书,存入list
        List<Book> list = bookList.stream().filter(book -> book.getId() % 2 == 0).collect(Collectors.toList());
        // 取出所有标题长度大于5的书,存入list
        Set<Book> set = bookList.stream().filter(book -> book.getTitle().length() > 5).collect(Collectors.toSet());

count、averaging、summarizing、max、sum

        // 统计书籍总数
        Long bookCount = bookList.stream().filter(book -> "天乔巴夏".equals(book.getAuthor())).count();

        // 求平均价格
        Double average = bookList.stream().collect(Collectors.averagingDouble(Book::getPrice));

        // 求最贵价格
        Optional<Integer> max = bookList.stream().map(Book::getPageCount).max(Double::compare);

        // 求价格之和
        Integer priceCount = bookList.stream().mapToInt(Book::getPageCount).sum();

        // 一次性统计所有信息
        DoubleSummaryStatistics c = bookList.stream().collect(Collectors.summarizingDouble(Book::getPrice));

group

        // 按书的价格是否高于100分组
        Map<Boolean,List<Book>> part = bookList.stream().collect(Collectors.partitioningBy(book -> book.getPrice() > 100));

        for (Map.Entry<Boolean,List<Book>> entry : part.entrySet()) {
            if (entry.getKey().equals(Boolean.TRUE)) {
                System.out.println("price > 100 ==> " + entry.getValue());
            } else {
                System.out.println("price <= 100 <== " + entry.getValue());
            }
        }

        // 按页数分组
        Map<Integer,List<Book>> group = bookList.stream().collect(Collectors.groupingBy(Book::getPageCount));
        System.out.println(group);

join

        // 获取所有书名
        String titles = bookList.stream().map(Book::getTitle).collect(Collectors.joining(","));
        System.out.println("所有书名 : " + titles);

sort

        // 按价格升序
        List<Book> sortListByPrice = bookList.stream().sorted(Comparator.comparing(Book::getPrice)).collect(Collectors.toList());
        System.out.println(sortListByPrice);
        // 按价格降序
        List<Book> sortListByPriceReversed = bookList.stream().sorted(Comparator.comparing(Book::getPrice).reversed()).collect(Collectors.toList());
        System.out.println(sortListByPriceReversed);

        // 先价格再页数
        List<Book> sortListByPriceAndPageCount = bookList.stream().sorted(Comparator.comparing(Book::getPrice)
                .thenComparing(Book::getPageCount)).collect(Collectors.toList());
        System.out.println(sortListByPriceAndPageCount);

distinct、concat、limit、skip

        Stream<Integer> stream1 = Stream.of(1,3,4);
        Stream<Integer> stream2 = Stream.of(2,4,5,5);
        // 合并
        List<Integer> concatList = Stream.concat(stream1,stream2).collect(Collectors.toList());
        System.out.println(concatList);

        // 去重
        List<Integer> distinctList = concatList.stream().distinct().collect(Collectors.toList());
        System.out.println(distinctList);

        // 限制
        List<Integer> limitList = distinctList.stream().limit(3).collect(Collectors.toList());
        System.out.println(limitList);

        // 跳过
        List<Integer> skipList = limitList.stream().skip(1).collect(Collectors.toList());
        System.out.println(skipList);

        // 迭代
        List<Integer> iterateList = Stream.iterate(1,x -> x + 2).limit(10).collect(Collectors.toList());
        System.out.println(iterateList);

        // 生成
        List<Integer> generateList = Stream.generate(() -> new Random().nextInt()).limit(5).collect(Collectors.toList());
        System.out.println(generateList);

@R_ 747_ 2419@ed

Int [] array is quickly converted to list.

      int[] arr = {1,5};
      List<Integer> res = Arrays.stream(arr).@R_747_2419@ed().collect(Collectors.toList());
      System.out.println(res);

Int [] array is converted to list or array in reverse order.

      int[] arr = {1,5};
      int[] res = Arrays.stream(arr)
              .@R_747_2419@ed() // 装箱
              .sorted(Comparator.reverSEOrder())
              .mapToInt(i -> i) // 转为IntStream
              .toArray();

      List<Integer> collect = Arrays.stream(arr)
              .@R_747_2419@ed()
              .sorted(Comparator.reverSEOrder())
              .collect(Collectors.toList());

      Integer[] r = Arrays.stream(arr)
              .@R_747_2419@ed()
              .sorted(Comparator.reverSEOrder())
              .toArray(Integer[]::new);

Reference reading

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>