java8-9-Stream 的中間操作


 
 
Stream 的中間操作
 
filter 過濾 排除元素   

 filter(T -> boolean)

保留 boolean 為 true 的元素

 
limit 截斷 取得多少個結果

limit(long n)

返回前 n 個元素

 
 skip 跳過幾個結果  數量不足 返回空

skip(long n)

去除前 n 個元素

 
 
distinct 去重復

distinct()

去除重復元素,這個方法是通過類的 equals 方法來判斷兩個元素是否相等的

 
 
                    map 根據Lambda體中的操作 提取元素到 流中
                             若Lambdda體操作返回Stream()流
                             結果會是兩層流    需要遍歷兩次
 
flapMap  平鋪map  將元素提取到一個流中
 
 

sorted() / sorted((T, T) -> int)

如果流中的元素的類實現了 Comparable 接口,即有自己的排序規則,那么可以直接調用 sorted() 方法對元素進行排序,如 Stream

反之, 需要調用 sorted((T, T) -> int) 實現 Comparator 接口

1.自然排序  java.lang.Comparable
 
 
2.自然排序 java.util.Comparator
 
 
 
 
 
  1 package com.wf.zhang.java8.stream;
  2 
  3 import com.wf.zhang.java8.lamdba.Employee;
  4 import org.junit.Test;
  5 
  6 import java.util.ArrayList;
  7 import java.util.Arrays;
  8 import java.util.List;
  9 import java.util.Objects;
 10 import java.util.stream.Stream;
 11 
 12 public class TestStream2 {
 13 
 14     //准備數據
 15     List<Employee> list = Arrays.asList(
 16             new Employee("張飛", 18, 5000.00),
 17             new Employee("趙雲", 28, 6666.66),
 18             new Employee("關羽", 38, 7777.77),
 19             new Employee("曹阿蠻", 29, 7777.77),
 20             new Employee("劉備", 48, 10000.88),
 21             new Employee("劉備", 48, 10000.88),
 22             new Employee("劉備", 48, 10000.88),
 23             new Employee("劉備", 48, 10000.88),
 24             new Employee("諸葛亮", 26, 10000.88)
 25     );
 26 
 27 
 28     /**
 29      * stream 中間操作  篩選
 30      * <p>
 31      * filter  過濾   排除元素
 32      * limit   截斷   取得幾個結果
 33      * skip    跳過幾個結果  若是數量不足 返回空
 34      * distinct  去重復 必須重寫 hashCode() 和 equals()
 35      */
 36     @Test
 37     public void test01() {
 38         list.stream().filter((t) -> t.getSalary() > 5000)
 39                 .forEach(System.out::println);
 40     }
 41 
 42     @Test
 43     public void test02() {
 44         list.stream().filter((t) -> t.getAge() > 20)
 45                 .limit(1)
 46                 .forEach(System.out::println);
 47     }
 48 
 49     @Test
 50     public void test03() {
 51         list.stream().filter((t) -> t.getSalary() > 5000)
 52                 .skip(2)
 53                 .forEach(System.out::println);
 54     }
 55 
 56     @Test
 57     public void test04() {
 58         list.stream().filter((t) -> t.getSalary() > 5000)
 59                 .skip(2)
 60                 .distinct()
 61                 .forEach(System.out::println);
 62     }
 63 
 64     /**
 65      * stream 中間操作  映射
 66      * <p>
 67      * map 根據Lambda體中的操作  提取元素  到流中
 68      * 若Lambda體操作中返回stream()流  會是兩層stream()流
 69      * 需要遍歷兩次
 70      * <p>
 71      * flatMap 將元素 添加到 新的一個流中
 72      */
 73     @Test
 74     public void test05() {
 75 
 76         List<String> stringList = Arrays.asList("aaa", "bbb", "ccc");
 77         stringList.stream()
 78                 .map((s) -> s.toUpperCase())
 79                 .forEach(System.out::println);
 80 
 81         System.out.println("----------------------------");
 82 
 83         //Employee的集合按照名字提取出來
 84         list.stream()
 85                 .map(Employee::getName)
 86                 .forEach(System.out::println);
 87 
 88 
 89         System.out.println("----------------------------");
 90 
 91 
 92         //將stringList 調用 filterchar方法
 93         Stream<Stream<Character>> stream = stringList.stream()
 94                 .map(TestStream2::filterchar);
 95         //map
 96         // 兩層 stream 流遍歷
 97         stream.forEach((sm) -> {
 98             sm.forEach(System.out::println);
 99         });
100 
101         System.out.println("+++++++++++++++++++++++++++++++++++++");
102 
103 
104         Stream<Character> characterStream = stringList.stream()
105                 .flatMap(TestStream2::filterchar);
106         //flatMap 一層遍歷
107         characterStream.forEach(System.out::println);
108     }
109 
110     /**
111      * 定義一個方法 將字符串轉成字符數組 提取一個個字符的 Stream流
112      *
113      * @param str
114      * @return
115      */
116     public static Stream<Character> filterchar(String str) {
117         List<Character> charlist = new ArrayList<>();
118         for (Character ch : str.toCharArray()) {
119             charlist.add(ch);
120         }
121         return charlist.stream();
122     }
123 
124     /***
125      * stream 中間操作   排序
126 
127      *                         1 自然排序:java.lang.Comparable  Comparable接口   compareTo()方法
128      *
129      *                        2 定制排序:java.util.Compartor   Comparator接口   compare()方法
130      */
131     @Test
132     public void test0e6() {
133 
134         List<String> stringList = Arrays.asList("fff","aaa", "bbb", "ccc");
135         //1 自然排序
136                  stringList.stream()
137                            .sorted()
138                            .forEach(System.out::println);
139 
140         System.out.println("---------------------------------");
141 
142         //2 定制排序
143                  list.stream()
144                      .sorted((x, y) ->{
145                          if (Objects.equals(x.getAge(),y.getAge())) {
146                              return x.getName().compareTo(y.getName());
147                          }else  {
148                                return Integer.compare(x.getAge(),y.getAge());
149                          }
150                      }).forEach(System.out::println);
151     }
152 
153 
154 }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM