最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 8 lambda表達式引入詳解及實例

 更新時間:2017年05月28日 09:53:58   作者:蹭飯熊  
這篇文章主要介紹了Java 8 lambda表達式引入詳解及實例的相關資料,需要的朋友可以參考下

Java 8 lambda表達式引入詳解及實例

eclipse 下載安裝

Help -> EclipseMarketplace -> 搜索Java 8 Kepler ->Java 8 support for eclipse Kepler SR2 安裝完成后需要重啟

Android Studio

在project的build.gradle文件中添加

buildscript {
  dependencies {
    classpath 'me.tatarka:gradle-retrolambda:3.2.5'
  }
}

在app的build.gradle文件中添加

apply plugin: 'me.tatarka.retrolambda'

android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

 

使用

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.Stream.Builder;
 
public class LambdaTest {
 
 public static void main(String[] args) {
 
  String[] str = new String[] { "Lambdas", "Lambdas", "Default Method", "Stream API", "Date and Time API" };
  List<String> strList = Arrays.asList(str);
 
  System.out.println("----------------------------> 默認遍歷");
  strList.stream().forEach(item -> {
   System.out.println(item);
  });
  System.out.println("----------------------------> 默認遍歷簡化寫法");
  strList.stream().forEach(System.out::println);
 
  // limit輸出指定個數(shù)
  System.out.println("limit ---------------------------->");
  strList.stream().limit(2).forEach(System.out::println);
 
  // 去掉重復數(shù)據(jù)
  System.out.println("distinct ---------------------------->");
  strList.stream().distinct().forEach(System.out::println);
 
  // filter過濾器,篩選出符合條件的值
  System.out.println("filter ---------------------------->");
  Predicate<String> contain = item -> item.contains("API");// 只是用于匹配條件的如int可以用條件運算符等
  strList.stream().filter(contain).forEach(System.out::println);
  System.out.println("filter簡化寫法 ---------------------------->");
  strList.stream().filter(item -> item.contains("API")).forEach(System.out::println);
 
  System.out.println("AND ---------------------------->");
  Predicate<String> contain1 = item -> item.contains("API");
  Predicate<String> contain2 = item -> item.contains("Time");
  strList.stream().filter(contain1.and(contain2)).forEach(System.out::println);
  System.out.println("OR ---------------------------->");
  strList.stream().filter(contain1.or(contain2)).forEach(System.out::println);
 
  // 向每個字符后追加
  System.out.println("map ---------------------------->");
  // 對Stream中包含的元素使用給定的轉換函數(shù)進行轉換操作,生成的Stream只包含轉換生成的元素。
  // mapToInt,mapToLong和mapToDouble是對int、long、double進行操作的
  strList.stream().map(item -> item + String.valueOf(1)).forEach(System.out::println);
 
  // 向每個字符后追加
  System.out.println("flatMap ---------------------------->");
  // flatMap:和map類似,不同的是其每個元素轉換得到的是Stream對象,會把子Stream中的元素壓縮到父集合
  strList.stream().flatMap(item -> getCharacter(item)).forEach(System.out::println);
 
  System.out.println("peek ---------------------------->");
  // peek 需調(diào)用collect
  strList.stream().map(String::toUpperCase).peek(System.out::println).collect(Collectors.toList());
 
  System.out.println("skip ---------------------------->");
  // 丟棄原Stream的前N個元素后剩下元素組成的新Stream
  strList.stream().skip(3).forEach(System.out::println);
 
  // 統(tǒng)計個數(shù)
  System.out.println("count ---------------------------->" + strList.stream().count());
 
  // allMatch:是不是Stream中的所有元素都滿足給定的匹配條件
  boolean allMatch1 = strList.stream().allMatch(item -> item.contains("a"));
  System.out.println("allMatch --------------> " + allMatch1);
  boolean allMatch2 = strList.stream().allMatch(item -> item.contains("API"));
  System.out.println("allMatch --------------> " + allMatch2);
 
  // anyMatch:Stream中是否存在任何一個元素滿足匹配條件
  boolean anyMatch1 = strList.stream().anyMatch(item -> item.contains("Stream API"));
  System.out.println("anyMatch --------------> " + anyMatch1);
  boolean anyMatch2 = strList.stream().anyMatch(item -> item.contains("Stream API1"));
  System.out.println("anyMatch --------------> " + anyMatch2);
 
  // findFirst: 返回Stream中的第一個元素,如果Stream為空,返回空Optional
  Optional<String> findFirst = strList.stream().findFirst();
  // isPresent方法用來檢查Optional實例是否有值。
  if (findFirst.isPresent()) {
   // 調(diào)用get()返回Optional值。如果Optional沒有值調(diào)和則拋出NoSuchElementException。
   System.out.println("findFirst --------------> " + findFirst.get());
  }
  System.out.print("findFirst簡化寫法 --------------> ");
  // 如果存在值,則使用該值調(diào)用,否則不執(zhí)行任何操作。
  strList.stream().findFirst().ifPresent(System.out::println);
 
  // noneMatch:是不是Stream中的所有元素都不滿足給定的匹配條件
  boolean noneMatch1 = strList.stream().noneMatch(item -> item.contains("Stream API"));
  System.out.println("noneMatch --------------> " + noneMatch1);
  boolean noneMatch2 = strList.stream().noneMatch(item -> item.contains("zzzzz"));
  System.out.println("noneMatch --------------> " + noneMatch2);
 
  System.out.println("newStrList ---------------------------->");
  List<String> newStrList = strList.stream().filter(item -> item != null)
    .collect(() -> new ArrayList<String>(), (list, item) -> list.add(item), (list1, list2) -> list1.addAll(list2));
  newStrList.stream().forEach(System.out::println);
  System.out.println("newStrList簡化寫法 ---------------------------->");
  List<String> newStrList1 = strList.stream().filter(item -> item != null).collect(Collectors.toList());
  newStrList1.stream().forEach(System.out::println);
 
  System.out.println("sorted 排序---------------------------->");
  // strList.stream().sorted();
  strList.stream().sorted(Comparator.comparing(String::length)).forEach(System.out::println);
  ;
 
  // max和min:使用給定的比較器(Operator),返回Stream中的最大|最小值
  Integer[] ints = new Integer[] { 7, 2, 3, 10, 5, 1, 6, 8, 9, 4 };
  List<Integer> intList = new ArrayList<Integer>();
  intList = Arrays.asList(ints);
  System.out.print("max --------------> ");
  intList.stream().max((o1, o2) -> o1.compareTo(o2)).ifPresent(System.out::println);
  System.out.print("max簡化寫法 --------------> ");
  // Comparable<Integer> Integer.compare(int arg0, int arg1)
  intList.stream().max(Integer::compare).ifPresent(System.out::println);
  System.out.print("min --------------> ");
  intList.stream().min((o1, o2) -> o1.compareTo(o2)).ifPresent(System.out::println);
  System.out.print("min簡化寫法 --------------> ");
  // Comparable<Integer> Integer.compare(int arg0, int arg1)
  intList.stream().min(Integer::compare).ifPresent(System.out::println);
 
  System.out.println("reduce單參數(shù) ---------------------------->");
  System.out.println(intList.stream().reduce((result, element) -> result = result + element));
  System.out.println("reduce雙參數(shù) ---------------------------->");
  System.out.println(intList.stream().reduce(0, (result, element) -> result = result + element));
 
  System.out.println("generate ---------------------------->");
  // 生成一個無限長度的Stream,其中值是隨機的。這個無限長度Stream是懶加載,一般這種無限長度的Stream都會配合Stream的limit()方法來用。
  Stream.generate(Math::random).limit(2).forEach(System.out::println);
 
  System.out.println("iterate ---------------------------->");
  // 也是生成無限長度的Stream,和generator不同的是,其元素的生成是重復對給定的種子值,調(diào)用用戶指定函數(shù)來生成的
  Stream.iterate(12, item -> item + 1).limit(2).forEach(System.out::println);
 }
 
 public static Stream<String> getCharacter(String s) {
  Builder<String> builder = Stream.builder();
  builder.add(s);
  builder.accept("1");
  return builder.build();
 }
}
  

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • Java切面(Aspect)的多種實現(xiàn)方式

    Java切面(Aspect)的多種實現(xiàn)方式

    這篇文章主要給大家介紹了關于Java切面(Aspect)的多種實現(xiàn)方式,在Java開發(fā)中切面(Aspect)是一種常用的編程方式,用于實現(xiàn)橫切關注點(cross-cutting concern),需要的朋友可以參考下
    2023-08-08
  • Jenkins插件pipeline原理及使用方法解析

    Jenkins插件pipeline原理及使用方法解析

    這篇文章主要介紹了Jenkins插件pipeline原理及使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • Java 實現(xiàn)多線程切換等待喚醒交替打印奇偶數(shù)

    Java 實現(xiàn)多線程切換等待喚醒交替打印奇偶數(shù)

    這篇文章主要介紹了Java 實現(xiàn)多線程切換等待喚醒交替打印奇偶數(shù) ,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • java如何根據(jù)時間戳生成有序ID

    java如何根據(jù)時間戳生成有序ID

    這篇文章主要介紹了java如何根據(jù)時間戳生成有序ID問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Struts中使用validate()輸入校驗方法詳解

    Struts中使用validate()輸入校驗方法詳解

    這篇文章主要介紹了Struts中使用validate()輸入校驗方法,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-09-09
  • Java集合框架ArrayList源碼分析(一)

    Java集合框架ArrayList源碼分析(一)

    這篇文章主要為大家詳細介紹了Java集合框架ArrayList源碼分析,感興趣的小伙伴們可以參考一下
    2016-08-08
  • springboot?@Async?注解如何實現(xiàn)方法異步

    springboot?@Async?注解如何實現(xiàn)方法異步

    這篇文章主要介紹了springboot?@Async?注解如何實現(xiàn)方法異步,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring Boot 如何解決富文本上傳圖片跨域問題

    Spring Boot 如何解決富文本上傳圖片跨域問題

    這篇文章主要介紹了Spring Boot 如何解決富文本上傳圖片跨域問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java/Web調(diào)用Hadoop進行MapReduce示例代碼

    Java/Web調(diào)用Hadoop進行MapReduce示例代碼

    本篇文章主要介紹了Java/Web調(diào)用Hadoop進行MapReduce示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Lombok @Slf4j log對象沒有info等方法不可用問題及解決

    Lombok @Slf4j log對象沒有info等方法不可用問題及解決

    本文主要介紹了如何解決Spring Boot項目中的日志依賴沖突問題,以及如何使用Lombok和SLF4J進行日志記錄,Lombok通過生成Logger對象簡化了日志記錄,而SLF4J提供了一個統(tǒng)一的日志接口,允許開發(fā)者在運行時選擇不同的日志實現(xiàn)
    2024-12-12

最新評論

曲松县| 阿巴嘎旗| 天气| 富裕县| 蒙自县| 体育| 菏泽市| 吉安市| 舟山市| 称多县| 高唐县| 富宁县| 保亭| 芦溪县| 长武县| 松阳县| 达日县| 阜康市| 承德市| 孝昌县| 广昌县| 江油市| 义马市| 若尔盖县| 两当县| 余庆县| 开远市| 象州县| 银川市| 从化市| 靖安县| 黄山市| 镇江市| 峨山| 乐亭县| 桐梓县| 牡丹江市| 陇川县| 五大连池市| 民丰县| 永寿县|