本文实例为大家分享了java中lambda常用场景的具体代码,供大家参考,具体内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
public class test18 { /** * lambda表达式的常用场景 */ @test public void test() { list<string> list_one = new arraylist<>(); list_one.add( "nike" ); list_one.add( "addidas" ); /** * 用在匿名内部类里简写 ()-> */ new thread( new runnable() { @override public void run() { system.out.println( "do nothing" ); } }).start(); new thread(() -> system.out.println( "do nothing" )).start();
//用在集合比较器里 collections.sort(list_one, new comparator<string>() { @override public int compare(string o1, string o2) { return o1测试数据pareto(o2); } }); collections.sort(list_one, (o1, o2) -> o1测试数据pareto(o2)); //正序 collections.sort(list_one, (o1, o2) -> -o1测试数据pareto(o2)); //逆序
//用在遍历集合 或操作集合中元素的时候 list_one.foreach(system.out::println); list result = new arraylist(); list_one.stream().foreach(item -> result.add(item));
system.out.println( "--boundary--" );
//通过自定义filter方法 或者 集合.stream().filter(predicate<>) filter(list_one, n -> n.startswith( "n" )); filter(list_one, n -> true ); filter(list_one, n -> false ); filter(list_one, n -> n.length() > 5 );
predicate<string> predicate = n -> n.startswith( "n" ); list_one.stream().filter(predicate).foreach(system.out::print); list<integer> list3 = new arraylist<>(); list3.add( 10 ); list3.add( 8 ); list3.add( 3 ); list3.add( 15 ); list3.add( 20 ); predicate<integer> predicate1 = n -> integer.valueof(n) > 10 ;
//用于map reduce中 此处先filter删选数据 然后执行map操作 double adouble = list3.stream().filter(predicate1).map(vo -> vo * . 12 + vo).reduce((sum, vo) -> sum + vo).get(); system.out.println(adouble);
//stream()之后可以通过distinct()去重 也可以通过stream().collect(collectors.toset())去重 //collector.joining(delimiter)通过定界符连接成字符串 list<string> list4 = new arraylist<>(); list4.add( "hello" ); list4.add( "boy" ); list4.add( "how" ); list4.add( "are" ); list4.add( "you" ); list4.add( "you" ); list4.stream().distinct().collect(collectors.tolist()).foreach(system.out::print); system.out.println(list4.stream().map(x -> x.touppercase()).collect(collectors.joining( ", " )));
/** * 通过maptoint() maptodouble() maptolong() 然后summarstatistics() 可以获得 intsummarystatistics 这个类的实例 * 然后调用它的getsum() getaverage() getcount() getmax() getmin()方法 */ list<integer> list5 = arrays.aslist( 1 , 3 , 4 , 5 , 6 , 7 , 10 , 23 ); intsummarystatistics intsummarystatistics = list5.stream().maptoint(x -> x).summarystatistics(); system.out.println(intsummarystatistics.getsum()); system.out.println(intsummarystatistics.getaverage()); system.out.println(intsummarystatistics.getcount()); system.out.println(intsummarystatistics.getmax()); system.out.println(intsummarystatistics.getmin()); }
public void filter(list<string> names, predicate<string> condition) { names.stream().filter(name -> condition.test(name)).foreach(vo -> system.out.print(vo + " " )); system.out.println(); } }
|
以上所述是小编给大家介绍的[java中lambda常用场景详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
原文链接:https://blog.csdn.net/qq_34557770/article/details/89228928
查看更多关于java中Lambda常用场景代码实例的详细内容...