map

可以把流中的元素进行计算或转换

  
      // 类型转换
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .map(author -> author.getName())
                  .forEach(s -> System.out.println(s));
      }
  ​
      // 所有作家年龄加上10再返回
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .map(author -> author.getAge())
                  .map(age -> age + 10)
                  .forEach(age -> System.out.println(age));
      }

<br/>

distinct

去重操作<br/>

打印所有作家的姓名,并且要求不能有重复元素

注意 : distinct方法式依赖Object的equals方法判断是否是同一个对象,要注意重写equals方法

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .distinct()
                  .forEach(author -> System.out.println(author));
      }
sorted

可以对流当中的元素进行排序<br/>按年龄降序排序,不能重复

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .distinct()
                  .sorted((o1, o2) -> o1.getAge() - o2.getAge())
                  .forEach(author -> System.out.println(author.getAge()));
      }
limit

设置流的最大长度,超出的部分会被抛弃掉<br/>对年龄降序排序,然后打印年龄最大的两个作家

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .distinct()
                  .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                  .limit(2)
                  .forEach(author -> System.out.println(author.getAge()));
      }
skip

跳过流的前n个元素 返回剩下的元素<br/>打印除了年龄最大的作家之外的作家

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .distinct()
                  .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                  .skip(1)
                  .forEach(author -> System.out.println(author.getAge()));
      }
flatMap

可以把一个对象转换为多个对象<br/>打印所有书籍的名字,去重

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .flatMap( author -> author.getBookList().stream())
                  .distinct()
                  .forEach(book -> System.out.println(book.getName()));
      }

<br/>打印现有书籍的所有分类,去重,不也能包括"哲学,爱情"

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .flatMap(author -> author.getBookList().stream())
                  .distinct()
                  .flatMap(book -> Arrays.stream(book.getCategory().split(",")))
                  .distinct()
                  .forEach(s -> System.out.println(s));
      }

<br/>

3. 终结操作

forEach

打印所有作家名

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          authors.stream()
                  .map(author -> author.getName())
                  .distinct()
                  .forEach(name -> System.out.println(name));
      }

<br/>

count

获取元素的个数<br/>输出作家的书籍个数,去重

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          long count = authors.stream()
                  .flatMap(author -> author.getBookList().stream())
                  .distinct()
                  .count();
          System.out.println(count);
      }
max min

求流当中的最值<br/>

获取作家书籍的最高分和最低分

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          Optional<Double> max = authors.stream()
                  .flatMap(author -> author.getBookList().stream())
                  .map(book -> book.getScore())
                  .max((o1, o2) -> o1.intValue() - o2.intValue());
          System.out.println(max.get());
      }
collect

把流再转换成集合<br/>获取存放所有作者名字的list集合

  
          List<Author> authors = getAuthors();
          List<String> collect = authors.stream()
                  .map(author -> author.getName())
                  .collect(Collectors.toList());
          System.out.println(collect);
      }

<br/>把所有书名转换成set集合

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          Set<Book> nameSet = authors.stream()
                  .flatMap(author -> author.getBookList().stream())
                  .collect(Collectors.toSet());
          System.out.println(nameSet);
      }

<br/>作者名为key,List<Book> 为value,转换成map集合

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          Map<String, List<Book>> map = authors.stream()
                  .distinct()
                  .collect(Collectors.toMap(author -> author.getName(), author -> author.getBookList()));
          System.out.println(map);
      }
  ````
  ​
  #### anyMatch
  判断是否有符合条件的元素
  <br/>
  判断是否有年龄29以上的
  ```java
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          boolean b = authors.stream()
                  .anyMatch(author -> author.getAge() > 29);
          System.out.println(b);
      }

<br/>

allMatch

判断是否所有的都匹配<br/>

判断是否所有的作家都是成年人

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          boolean b = authors.stream()
                  .allMatch(author -> author.getAge() >= 18);
          System.out.println(b);
      }

<br/>

noneMatch

判断是不是都不符合<br/>判断作家是否都不超过100岁

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          boolean b = authors.stream()
                  .noneMatch(author -> author.getAge() > 100);
          System.out.println(b);
      }
findAny

获取任意一个元素<br/>

查找任意一个大于18的作家<br/>

  
          List<Author> authors = getAuthors();
          Optional<Author> any = authors.stream()
                  .filter(author -> author.getAge() > 18)
                  .findAny();
          any.ifPresent(author -> System.out.println(author.getName()));
      }

<br/>

findAny

获取第一个元素<br/>

获取年龄最小的作家<br/>

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          Optional<Author> first = authors.stream()
                  .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                  .findFirst();
          first.ifPresent(author -> System.out.println(author.getAge()));
      }
reduce

对流中的数据按制定的计算结果给出结果<br/>求所有年龄的和

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          Integer reduce = authors.stream()
                  .map(author -> author.getAge())
                  .reduce(0, (result, integer2) -> result + integer2);
          System.out.println(reduce);
      }

用reduce求年龄的最大值

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          Integer max = authors.stream()
                  .map(author -> author.getAge())
                  .reduce(Integer.MIN_VALUE, (result, integer2) -> result > integer2 ? result : integer2);
          System.out.println(max);
      }

求年龄的最小值

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          Integer min = authors.stream()
                  .map(author -> author.getAge())
                  .reduce(Integer.MAX_VALUE, (result, integer2) -> result > integer2 ? integer2 : result);
          System.out.println(min);
      }

一个参数的重载形式

  
      public static void main(String[] args) {
          List<Author> authors = getAuthors();
          Optional<Integer> reduce = authors.stream()
                  .map(author -> author.getAge())
                  .reduce((integer, integer2) -> integer > integer2 ? integer : integer2);
          System.out.println(reduce.get());
      }
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐