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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java -Stream流和常见函数式接口

發布時間:2023/12/29 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java -Stream流和常见函数式接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概念

流(Stream)與集合類似,但集合中保存的是數據,而Stream中保存對集合或數組數據的操作。

特點

  • tream 自己不會存儲元素。
  • Stream 不會改變源對象。相反,他們會返回一個持有結果的新Stream。
  • Stream 操作是延遲執行的,會等到需要結果的時候才執行。

使用

創建:

  • 新建一個流。

中間操作:

  • 在一個或多個步驟中,將初始Stream轉化到另一個Stream的中間操作。

終止操作:

  • 使用一個終止操作來產生一個結果。該操作會強制之前的延遲操作立即執行,在此之后,該Stream就不能使用了。

創建方式:

  • 通過Collection對象的stream()或parallelStream()方法。
  • 通過Arrays類的stream()方法。
  • 通過Stream接口的of()、iterate()、generate()方法。
  • 通過IntStream、LongStream、DoubleStream接口中的of、range、rangeClosed方法。
    創建
List<String> list=new ArrayList<>();list.add("111");list.add("222");list.add("33");//獲取stream對象1Stream<String> stream = list.stream();stream.forEach(System.out::println);//獲取stream對象2Stream<String> stringStream = Stream.of("222", "444", "555");stringStream.forEach(System.out::println);

中間操作:

//中間操作//filter 過濾 傳入參數是一個斷言型接口 獲取集合中長度大于2的元素list.stream().filter(s -> s.length() > 2).forEach(System.out::println);//limit 和mysql的limit相似 獲取集合的前1條數據list.stream().limit(1).forEach(System.out::println);//distinct 去重list.stream().distinct().forEach(System.out::println);//sorted 排序 傳入一個比較器 和list的sort方法相似 將集合元素按長度從小到大排序list.stream().sorted((o1,o2)->o1.length()-o2.length());//map 傳入一個函數型接口 將集合中的元素全部轉換成Int型list.stream().map(Integer::parseInt).forEach(System.out::println);//parallel 使用多線程操作集合中的元素list.stream().parallel().forEach((s)->{System.out.println(Thread.currentThread().getName()+" "+s);});

終止操作:

//終止操作//min 獲取集合中最小元素 最小的判斷是按照比較器來的 就是輸出經過比較器排序后的第一個元素Optional<String> min = list.stream().min((o1, o2) -> {return o1.length() - o2.length();});System.out.println(min.get());//max 獲取集合中最大元素 與min機制相同 是輸出最后一個Optional<String> max = list.stream().max((o1, o2) -> {return o1.length() - o2.length();});System.out.println(max.get());//count 計數 獲取長度為3的元素的個數long count = list.stream().filter(s -> s.length() == 3).count();System.out.println(count);//reduce 將元素轉換為int并求和list.stream().map(Integer::parseInt).reduce(0,(o1,o2)->o1+o2);//collect 將stream流對象轉換為List Set Map集合 將流轉換為listList<String> collect = list.stream().collect(Collectors.toList());collect.forEach(System.out::println);

常見函數式接口

  • Consumer 消費型接口 參數為T 返回void ;
    void accept(T t)對類型為T的對象應用操作
  • public class MyConsumer {public static void test(String str, Consumer<String> consumer){consumer.accept(str);}public static void main(String[] args) {MyConsumer.test("hello",str-> System.out.println(str));test("hello",System.out::println);} }
  • Supplier 供給型接口
    參數無 返回值為T
    T get();返回類型為T的對象
  • public class MySupplier {public static void main(String[] args) {System.out.println(test(() -> {return "hello";}));}public static String test(Supplier<String> supplier){return supplier.get();} }

    3.Function<T,R>函數型接口
    參數T 返回值R
    R apply(T t) 對類型為T的對象應用操作,并返回類型為R類型的對象

    public class MyFunction {public static void main(String[] args) {System.out.println(test("hello", str -> {return str;}));}public static String test(String s, Function<String,String> f){return f.apply(s);} }

    4.Predicate 斷言型接口
    參數T 返回值boolean
    boolean test(T t);確定類型為T的對象是否滿足條件 滿足返回true

    public class Predicate {public static void main(String[] args) {System.out.println(test("hello", str -> {return "hello".equals(str);}));}public static boolean test(String s, java.util.function.Predicate<String> p){return p.test(s);} }

    總結

    以上是生活随笔為你收集整理的Java -Stream流和常见函数式接口的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。