日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA8 Stream方法使用详解Filter、map等用法(一)

發(fā)布時間:2025/1/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA8 Stream方法使用详解Filter、map等用法(一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 一、篩選和切片
      • 1、謂詞篩選filter
      • 2、篩選不同的元素distinct
      • 3、截斷流limit
      • 4、跳過元素
    • 二、映射
      • 1、map對每個元素應(yīng)用函數(shù)
      • 2、流的扁平化
    • 三、查找和匹配
      • 1、至少匹配一個
      • 2、匹配所有
      • 3、查找元素
      • 4、查找第一個元素

流可以讓我們從外部迭代轉(zhuǎn)向內(nèi)部迭代,流可以理解為按需加載(只有消費者消費的時候才開始生產(chǎn)),集合是供應(yīng)商驅(qū)動(先把倉庫裝滿,再開始賣)。流可以看作在時間中分布的一組,集合則是在空間中分布的一組。

以下例子都用此數(shù)據(jù):

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));

一、篩選和切片

用謂詞來篩選各不相同的元素,或?qū)⒘鹘財嘀付ǖ拈L度

1、謂詞篩選filter

Stream接口支持filter方法,該方法接受一個謂詞(返回Boolean的函數(shù))作為參數(shù),例:

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

該方法會給定一個不超過指定長度,所需的長度作為參數(shù)傳遞給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);}

二、映射

一個非常常見的數(shù)據(jù)處理就是從某些對象種選擇信息,比如在SQL里,你可以從表中選擇一列,Stream API也通過map和flatMap提供了類似的方法

1、map對每個元素應(yīng)用函數(shù)

流支持map方法,它接受一個函數(shù)作為參數(shù)。這個函數(shù)會被應(yīng)用到每個元素上,并映射成新的元素。說白了就是返回一個新的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各個數(shù)組并不是分別映射成一個流,而是映射成流的內(nèi)容,說白了就是把幾個小的list轉(zhuǎn)換到一個大的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);}


給定數(shù)字列表1[1,2,3]和列表2[3,4],返回[(1,3),(2,3),(2,3),(2,4),(3,3),(3,4)]

List<Integer> integers = Arrays.asList(1, 2, 3); List<Integer> integers1 = Arrays.asList(3, 4)List<int[]> collect3 = integers.stream().flatMap(i -> integers1.stream().map(j -> new int[]{i, j})).collect(Collectors.toList());System.out.println(collect3);

三、查找和匹配

另一個常見的數(shù)據(jù)處理套路是看著數(shù)據(jù)集中的某些元素是否匹配一個給定屬性。
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,是一個容器代表值存在不存在,這個類我們將在以后章節(jié)中詳細講解。

4、查找第一個元素

findFirst

public static void testFindAny() {Optional<Dish> collect = menu.stream().filter(dish -> dish.getCalories() > 1000).findFrist();System.out.println(collect);}

以上是部分API,我們下一章節(jié)繼續(xù)講解。

總結(jié)

以上是生活随笔為你收集整理的JAVA8 Stream方法使用详解Filter、map等用法(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。