JAVA8 Stream方法使用详解Filter、map等用法(一)
文章目錄
- 一、篩選和切片
- 1、謂詞篩選filter
- 2、篩選不同的元素distinct
- 3、截斷流limit
- 4、跳過元素
- 二、映射
- 1、map對每個元素應用函數
- 2、流的扁平化
- 三、查找和匹配
- 1、至少匹配一個
- 2、匹配所有
- 3、查找元素
- 4、查找第一個元素
流可以讓我們從外部迭代轉向內部迭代,流可以理解為按需加載(只有消費者消費的時候才開始生產),集合是供應商驅動(先把倉庫裝滿,再開始賣)。流可以看作在時間中分布的一組,集合則是在空間中分布的一組。
以下例子都用此數據:
public class Dish {private final String name;private final boolean vegetarian;private final int calories;private final Type type;public Dish(String name, boolean vegetarian, int calories, Type type) {this.name = name;this.vegetarian = vegetarian;this.calories = calories;this.type = type;}public String getName() {return name;}public boolean isVegetarian() {return vegetarian;}public int getCalories() {return calories;}public Type getType() {return type;}@Overridepublic String toString() {return name;} } public enum Type {MEAT, FISH, OTHER } private static List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Type.MEAT),new Dish("beef", false, 700, Type.MEAT),new Dish("chicken", false, 400, Type.MEAT),new Dish("french fries", true, 530, Type.OTHER),new Dish("rice", true, 350, Type.OTHER),new Dish("season fruit", true, 120, Type.OTHER),new Dish("pizza", true, 550, Type.OTHER),new Dish("prawns", false, 300, Type.FISH),new Dish("salmon", false, 450, Type.FISH));一、篩選和切片
用謂詞來篩選各不相同的元素,或將流截斷指定的長度
1、謂詞篩選filter
Stream接口支持filter方法,該方法接受一個謂詞(返回Boolean的函數)作為參數,例:
public static void testFilter(){List<Dish> collect = menu.stream().filter(dish -> dish.isVegetarian()).collect(Collectors.toList());System.out.println(collect);}2、篩選不同的元素distinct
distinct方法,返回一個元素各異的流,說白了就是去重。例:
public static void testDistinct() {List<String> strings = Arrays.asList("A", "B", "B", "C", "D", "D", "E");strings.stream().distinct().forEach(System.out::println);}3、截斷流limit
該方法會給定一個不超過指定長度,所需的長度作為參數傳遞給limit。例:
public static void testLimit() {List<String> strings = Arrays.asList("A", "B", "B", "C", "D", "D", "E");List<String> collect = strings.stream().limit(3).collect(Collectors.toList());System.out.println(collect);}4、跳過元素
流還支持跳過元素,返回扔掉前n個元素的流。例:
public static void testSkip() {List<String> strings = Arrays.asList("A", "B", "B", "C", "D", "D", "E");List<String> collect = strings.stream().skip(3).collect(Collectors.toList());System.out.println(collect);}二、映射
一個非常常見的數據處理就是從某些對象種選擇信息,比如在SQL里,你可以從表中選擇一列,Stream API也通過map和flatMap提供了類似的方法
1、map對每個元素應用函數
流支持map方法,它接受一個函數作為參數。這個函數會被應用到每個元素上,并映射成新的元素。說白了就是返回一個新的l類型的list集合。例:
map返回的是Stream(String)
public static void testMap() {List<String> collect = menu.stream().map(Dish::getName).collect(Collectors.toList());System.out.println(collect);}2、流的扁平化
flatMap各個數組并不是分別映射成一個流,而是映射成流的內容,說白了就是把幾個小的list轉換到一個大的list。例:
public static void testFlatMap() {String[] array = {"HELLO","WORLD"};Stream<String> stream = Arrays.stream(array);stream.forEach(System.out::println);List<String> strings = Arrays.asList("hello", "world");List<String[]> collect = strings.stream().map(w -> w.split("")).collect(Collectors.toList());System.out.println(collect);Stream<Stream<String>> streamStream = collect.stream().map(array1 -> Arrays.stream(array1));List<Stream<String>> collect1 = collect.stream().map(array1 -> Arrays.stream(array1)).collect(Collectors.toList());collect1.stream().forEach(d -> {d.forEach(System.out::println);});System.out.println(collect1);Stream<String> stringStream = strings.stream().map(w -> w.split("")).flatMap(Arrays::stream);List<String> collect2 = strings.stream().map(w -> w.split("")).flatMap(Arrays::stream).collect(Collectors.toList());System.out.println(collect2);}
給定數字列表1[1,2,3]和列表2[3,4],返回[(1,3),(2,3),(2,3),(2,4),(3,3),(3,4)]
三、查找和匹配
另一個常見的數據處理套路是看著數據集中的某些元素是否匹配一個給定屬性。
allMatch,、anyMatch、noneMatch、findFirst、findAny
1、至少匹配一個
anyMatch是否有一個元素匹配
if (menu.stream().anyMatch(Dish::isVegetarian)) {System.out.println("");}2、匹配所有
allMatch匹配所有
menu.stream().allMatch(d -> d.getCalories < 1000);noneMatch和allMatch是相對的沒有一個元素匹配
3、查找元素
findAny返回當前流任意元素
public static void testFindAny() {Optional<Dish> collect = menu.stream().filter(dish -> dish.getCalories() > 1000).findAny();System.out.println(collect);}返回值是一個Optional,是一個容器代表值存在不存在,這個類我們將在以后章節中詳細講解。
4、查找第一個元素
findFirst
public static void testFindAny() {Optional<Dish> collect = menu.stream().filter(dish -> dish.getCalories() > 1000).findFrist();System.out.println(collect);}以上是部分API,我們下一章節繼續講解。
總結
以上是生活随笔為你收集整理的JAVA8 Stream方法使用详解Filter、map等用法(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CompletableFuture AP
- 下一篇: JAVA8 Stream方法使用详解re