λ表达式怎么引入Java 8项目中?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
<强> Java 8λ表达式引入详解及实例强>
<强> eclipse下载安装强>
帮助→EclipseMarketplace→搜索Java 8开普勒→Java 8支持eclipse开普勒SR2安装完成后需要重启
<强> Android工作室强>
在项目的构建。gradle文件中添加
buildscript { 依赖关系{ 类路径& # 39;me.tatarka: gradle-retrolambda: 3.2.5& # 39; } }
在应用程序的构建。gradle文件中添加
应用插件:& # 39;me.tatarka.retrolambda& # 39; android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }
使用
进口java.util.ArrayList; 进口java.util.Arrays; 进口java.util.Comparator; 进口并不知道; 进口java.util.Optional; 进口java.util.function.Predicate; 进口java.util.stream.Collectors; 进口java.util.stream.Stream; 进口java.util.stream.Stream.Builder; 公开课LambdaTest { 公共静态void main (String [] args) { String [] str=new String [] {“Lambdas",“Lambdas",“默认Method",“流API",“API"日期和时间;}; ListstrList=arrays . aslist (str); System.out.println(“- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在默认遍历“); strList.stream ()。forEach(项目→{ System.out.println(项); }); System.out.println(“- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在默认遍历简化写法“); strList.stream () .forEach (system . out:: println);//限制输出指定个数 System.out.println(“限制- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“); strList.stream () .limit (2) .forEach (system . out:: println);//去掉重复数据 System.out.println(“不同的- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“); .distinct strList.stream () () .forEach (system . out:: println);//过滤过滤器、筛选出符合条件的值 System.out.println(“过滤器- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“); Predicate 包含=默认项目环境;item.contains (“API");//只是用于匹配条件的如int可以用条件运算符等 strList.stream () .filter(含).forEach (system . out:: println); System.out.println(“过滤器简化写法- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“); strList.stream ()。过滤器(项→item.contains (“API")) .forEach (system . out:: println); System.out.println(“; - - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“); Predicate 克制=项目→item.contains (“API"); Predicate contain2=项目→item.contains (“Time"); strList.stream () .filter (contain1.and (contain2)) .forEach (system . out:: println); System.out.println(“或- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“); strList.stream () .filter (contain1.or (contain2)) .forEach (system . out:: println);//向每个字符后追加 System.out.println(“地图- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“);//对流中包含的元素使用给定的转换函数进行转换操作,生成的流只包含转换生成的元素。//mapToInt mapToLong和mapToDouble是对int,长,双进行操作的 strList.stream ()。地图(项目→项+ String.valueOf (1) .forEach (system . out:: println);//向每个字符后追加 System.out.println (“flatMap - - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“);//flatMap:和地图类似,不同的是其每个元素转换得到的是流对象,会把子流中的元素压缩到父集合 strList.stream ()。flatMap(项目→getCharacter(项目)).forEach (system . out:: println); System.out.println (“peek - - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“);//peek需调用收集 strList.stream () . map(字符串::包含).peek (system . out:: println) .collect (Collectors.toList ()); System.out.println(“跳过- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“);//丢弃原流的前N个元素后剩下元素组成的新流 strList.stream () .skip (3) .forEach (system . out:: println);//统计个数 System.out.println(“数- - - - - - - - - - - - - - - - - - - - - - - - - - - - -在“+ strList.stream () .count ());//allMatch:是不是流中的所有元素都满足给定的匹配条件 布尔allMatch2=strList.stream ()。allMatch(项目→item.contains (“a")); System.out.println (“allMatch - - - - - - - - - - - - - - -在“;+ allMatch2); 布尔allMatch3=strList.stream ()。allMatch(项目→item.contains (“API")); System.out.println (“allMatch - - - - - - - - - - - - - - -在“;+ allMatch3);//anyMatch:流中是否存在任何一个元素满足匹配条件 布尔anyMatch2=strList.stream ()。anyMatch(项目→item.contains(“流API")); System.out.println (“anyMatch - - - - - - - - - - - - - - -在“;+ anyMatch2); 布尔anyMatch3=strList.stream ()。anyMatch(项目→item.contains(“流API1")); System.out.println (“anyMatch - - - - - - - - - - - - - - -在“;+ anyMatch3);//findFirst:返回流中的第一个元素,如果流为空,返回空可选的 Optional λ表达式怎么引入Java 8项目中