- 使用filter 根據 條件篩選 出結果:例如 找出 user 中 age >=15 的用戶
package lambda.stream; /** * @author 作者:cb * @version 創建時間:2019年1月4日 下午2:35:05 */ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamDemo { public static void main(String[] args) { // 1.集合創建stream對象 List<User> asList = Arrays.asList(new User("張三", 15), new User("李四", 25), new User("Tom", 10)); Stream<User> streamList = asList.stream(); // 找出 user age>15 的user collect(Collectors.toList()) 返回一個對應的集合 List<User> collect = streamList.filter(user -> user.getAge() >= 15).collect(Collectors.toList());
//集合提供的遍歷方法。。打印user對象 collect.forEach(user -> System.out.println(user)); } }public interface Stream<T> extends BaseStream<T, Stream<T>> { /** * Returns a stream consisting of the elements of this stream that match * the given predicate. * * <p>This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, * <a href="package-summary.html#Statelessness">stateless</a> * predicate to apply to each element to determine if it * should be included * @return the new stream */ Stream<T> filter(Predicate<? super T> predicate);
Stream<T> filter(Predicate<? super T> predicate) 接受的是一個 predicate 類型的接口參數。predicate 有一個test 的抽象boolean類型的返回值方法。該方法滿足條件age>=15 返回true 則程序 向下執行,執行結果:
User [name=張三, age=15] User [name=李四, age=25]
為了方便 所以 該測試類 每個步驟分開寫的,連着寫:
package lambda.stream; /** * @author 作者:cb * @version 創建時間:2019年1月4日 下午2:35:05 */ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamDemo { public static void main(String[] args) { // 1.集合創建stream對象 List<User> asList = Arrays.asList(new User("張三", 15), new User("李四", 25), new User("Tom", 10)); Stream<User> streamList = asList.stream(); // 找出 user age>15 的user .collect(Collectors.toList()) 返回一個對應的集合 // List<User> collect = streamList.filter(user -> user.getAge() >= // 15).collect(Collectors.toList()); // 集合提供的遍歷方法。。打印user對象 // collect.forEach(user -> System.out.println(user)); streamList.filter(user -> user.getAge() >= 15).collect(Collectors.toList()) .forEach(user -> System.out.println(user)); } }
- 使用stream distinct 去掉一個集合中的重復元素“:
package lambda.stream; /** * @author 作者:cb * @version 創建時間:2019年1月4日 下午2:35:05 */ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamDemo { public static void main(String[] args) { testDistinct(); } /** * 去重 */ public static void testDistinct() { List<Integer> asList = Arrays.asList(1, 2, 3, 5, 6, 7, 8, 8, 9, 7); List<Integer> collect = asList.stream().distinct().collect(Collectors.toList()); collect.forEach(System.out::print); } }
結果:
12356789
-
指定跳過多少個數據 類似 於 for循環里面的break 例如:
package lambda.stream; /** * @author 作者:cb * @version 創建時間:2019年1月4日 下午2:35:05 */ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamDemo { public static void main(String[] args) { testSkp(); } public static void testSkp() { List<Integer> asList = Arrays.asList(1, 2, 3, 5, 6, 7, 8, 9); List<Integer> collect = asList.stream().skip(5).collect(Collectors.toList()); collect.forEach(System.out::print); }
結果:
789
-
取一個集合的前幾條數據
package lambda.stream; /** * @author 作者:cb * @version 創建時間:2019年1月4日 下午2:35:05 */ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamDemo { public static void main(String[] args) { testLimit(); } public static void testLimit() { List<Integer> asList = Arrays.asList(1, 2, 3, 5, 6, 7, 8, 9); List<Integer> collect = asList.stream().limit(5).collect(Collectors.toList()); collect.forEach(System.out::print); }
結果:
12356
- 使用map 將集合中的每個數字 擴大 兩倍 :
package lambda.stream; /** * @author 作者:cb * @version 創建時間:2019年1月4日 下午2:35:05 */ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StreamDemo { public static void main(String[] args) { testMap(); } public static void testMap() { List<Integer> asList = Arrays.asList(1, 2, 3, 5, 6, 7, 8, 9); List<Integer> collect = asList.stream().map(i -> i * 2).collect(Collectors.toList()); collect.forEach(System.out::println); }
結果:
2 4 6 10 12 14 16 18
6 .解決一個字符串數組 返回單一的字符串使用flatMap:
package lambda.stream; /** * @author 作者:cb * @version 創建時間:2019年1月4日 下午2:35:05 */ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamDemo { public static void main(String[] args) { flatMap(); } public static void flatMap() { List<String> list = Arrays.asList("AABBBACCDED", "EDADCAJIONG "); list.stream().flatMap(item -> Arrays.stream(item.split(""))).distinct().collect(Collectors.toList()) .forEach(System.out::printf);
結果:
ABCDEJIONG
關於 map 和 flatMap的區別,可以了解一下。這里記錄一下自己的用法 畢竟寫這些 主要是給自己看 怕自己哪天忘了 回頭在看看