Lambda 表达式的常见用法
使用Lambda表达式集合遍历
List < String > fruitList = Arrays . asList ( "apple" , "banana" , "orange" , "mango" , "grape" ) ;
for ( String s : fruitList) {
System . out. println ( s) ;
}
fruitList. forEach ( System . out:: println ) ;
使用Lambda表达式排序
List < String > fruitList = Arrays . asList ( "apple" , "banana" , "orange" , "mango" , "grape" ) ;
Collections . sort ( fruitList, new Comparator < String > ( ) {
@Override
public int compare ( String o1, String o2) {
return o1. compareTo ( o2) ;
}
} ) ;
Collections . sort ( fruitList, ( ( o1, o2) -> o1. compareTo ( o2) ) ) ;
使用Lambda表达式过滤
List < String > fruitList = Arrays . asList ( "apple" , "banana" , "orange" , "mango" , "grape" ) ;
List < String > list1 = new ArrayList < > ( ) ;
for ( String s : fruitList) {
if ( s. startsWith ( "a" ) ) {
list1. add ( s) ;
}
}
List < String > list2 = fruitList. stream ( ) . filter ( s -> s. startsWith ( "a" ) ) . collect ( Collectors . toList ( ) ) ;
使用Lambda表达式映射
List < String > fruitList = Arrays . asList ( "apple" , "banana" , "orange" , "mango" , "grape" ) ;
List < Integer > list1 = new ArrayList < > ( ) ;
for ( String s : fruitList) {
list1. add ( s. length ( ) ) ;
}
List < Integer > list2 = fruitList. stream ( ) . map ( s -> s. length ( ) ) . collect ( Collectors . toList ( ) ) ;
使用Lambda表达式归约
List < Integer > sumList = Arrays . asList ( 1 , 2 , 3 , 4 , 5 , 6 , 8 ) ;
int sum1 = 0 ;
for ( Integer v : sumList) {
sum1 += v;
}
Integer sum2 = sumList. stream ( ) . reduce ( 0 , ( a, b) -> a + b) ;
使用Lambda表达式分组
List < String > fruitList = Arrays . asList ( "apple" , "banana" , "orange" , "mango" , "grape" ) ;
Map < Integer , List < String > > group1 = new HashMap < > ( ) ;
for ( String s : fruitList) {
if ( ! group1. containsKey ( s. length ( ) ) ) {
group1. put ( s. length ( ) , new ArrayList < > ( ) ) ;
}
group1. get ( s. length ( ) ) . add ( s) ;
}
Map < Integer , List < String > > group2 = fruitList. stream ( ) . collect ( Collectors . groupingBy ( String :: length ) ) ;
使用Lambda表达式函数式接口的实现
interface MyInterface {
public void doSomething ( String s) ;
}
MyInterface myInterface1 = new MyInterface ( ) {
@Override
public void doSomething ( String s) {
System . out. println ( s) ;
}
} ;
MyInterface myInterface2 = ( s) -> System . out. println ( s) ;
MyInterface myInterface3 = System . out:: println ;
使用Lambda表达式线程的创建
Thread thread = new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
System . out. println ( "Hello World" ) ;
}
} ) ;
thread. start ( ) ;
Thread thread1 = new Thread ( ( ) -> System . out. println ( "Hello World" ) ) ;
Thread thread2 = new Thread ( System . out:: println ) ;
Thread thread3 = new Thread ( LambdaUsefullness :: listreduceSum ) ;
ThreadPoolExecutor executor = new ThreadPoolExecutor ( 2 , 5 , 2L
, TimeUnit . SECONDS
, new LinkedBlockingDeque < > ( 4 )
, Executors . defaultThreadFactory ( )
, new ThreadPoolExecutor. AbortPolicy ( ) ) ;
executor. execute ( ( ) -> {
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( "Hello World分支线程====" + i) ;
}
} ) ;
使用Lambda表达式进行Optional 操作
String str = "Hello world" ;
if ( ! str. isEmpty ( ) ) {
System . out. println ( str. toUpperCase ( ) ) ;
}
Optional . ofNullable ( str) . map ( String :: toUpperCase ) . ifPresent ( System . out:: println ) ;
使用Lambda表达式进行Stream的流操作
List < String > fruitList = Arrays . asList ( "apple" , "banana" , "orange" , "mango" , "grape" ) ;
List list1 = new ArrayList ( ) ;
for ( String s : fruitList) {
if ( s. contains ( "n" ) ) {
list1. add ( s. toUpperCase ( ) ) ;
}
}
Collections . sort ( list1) ;
List < String > list2 = fruitList. stream ( ) . filter ( s -> s. contains ( "n" ) )
. map ( String :: toUpperCase ) . sorted ( ) . collect ( Collectors . toList ( ) ) ;