Lambda表达式
1.集合遍历
public class LambdaTest {
public static void main ( String [ ] args) {
List < String > list1 = Arrays . asList ( "aaa" , "bbb" , "ccc" ) ;
for ( int i = 0 ; i < list1. size ( ) ; i++ ) {
System . out. println ( list1. get ( i) ) ;
}
for ( String l : list1) {
System . out. println ( l) ;
}
list1. forEach ( s -> System . out. println ( s) ) ;
list1. forEach ( System . out:: println ) ;
}
}
2.集合排序
public class LambdaTest {
public static void main ( String [ ] args) {
List < String > list1 = Arrays . asList ( "aaa" , "bbb" , "ccc" ) ;
Collections . sort ( list1, new Comparator < String > ( ) {
@Override
public int compare ( String o1, String o2) {
return o1. compareTo ( o2) ;
}
} ) ;
Collections . sort ( list1, ( o1, o2) -> o1. compareTo ( o2) ) ;
}
}
3.过滤
public class LambdaTest {
public static void main ( String [ ] args) {
List < String > list1 = Arrays . asList ( "aaa" , "bbb" , "ccc" ) ;
List < String > list2 = new ArrayList < > ( ) ;
for ( String s : list1) {
if ( s. startsWith ( "a" ) ) {
list2. add ( s) ;
}
}
List < String > list3 = list1. stream ( ) . filter ( s -> s. startsWith ( "a" ) ) . collect ( Collectors . toList ( ) ) ;
}
}
4.映射
public class LambdaTest {
public static void main ( String [ ] args) {
List < String > list = Arrays . asList ( "apple" , "banana" , "peach" ) ;
List < Integer > list2 = new ArrayList < > ( ) ;
for ( String s : list) {
list2. add ( s. length ( ) ) ;
}
List < Integer > list3 = list. stream ( ) . map ( s -> s. length ( ) ) . collect ( Collectors . toList ( ) ) ;
}
}
5.求和
public class LambdaTest {
public static void main ( String [ ] args) {
List < Integer > list1 = Arrays . asList ( 2 , 3 , 5 ) ;
int sum1 = 0 ;
for ( Integer s : list1) {
sum1 += s;
}
int sum2 = list1. stream ( ) . reduce ( 0 , ( a, b) -> a + b) ;
}
}
6.分组
public class LambdaTest {
public static void main ( String [ ] args) {
List < String > list1 = Arrays . asList ( "aaa" , "bbb" , "ccc" , "ddddd" ) ;
Map < Integer , List < String > > map1 = new HashMap < > ( ) ;
for ( String i: list1) {
int length = i. length ( ) ;
if ( ! map1. containsKey ( length) ) {
map1. put ( length, new ArrayList < > ( ) ) ;
}
map1. get ( length) . add ( i) ;
}
Map < Integer , List < String > > map2 = list1. stream ( ) . collect ( Collectors . groupingBy ( String :: length ) ) ;
}
}
7.实现接口
public interface LambdaInterface {
void doSomething ( String s) ;
}
public class LambdaTest {
public static void main ( String [ ] args) {
LambdaInterface lambdaInterface = new LambdaInterface ( ) {
@Override
public void doSomething ( String s) {
System . out. println ( s) ;
}
} ;
lambdaInterface. doSomething ( "hello world" ) ;
LambdaInterface lambdaInterface12 = ( s) -> System . out. println ( s) ;
lambdaInterface12. doSomething ( "hello world" ) ;
}
}
8.创建线程
public class LambdaTest {
public static void main ( String [ ] args) {
Thread thread1 = new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
System . out. println ( "thread 01" ) ;
}
} ) ;
thread1. start ( ) ;
Thread thread2 = new Thread ( ( ) -> System . out. println ( "thread 02" ) ) ;
thread2. start ( ) ;
}
}
9.Optional判空操作(可以避免空指针)
public class LambdaTest {
public static void main ( String [ ] args) {
String str = "string" ;
if ( str != null ) {
System . out. println ( str. toUpperCase ( ) ) ;
}
Optional . ofNullable ( str) . map ( String :: toUpperCase ) . ifPresent ( System . out:: println ) ;
}
}
10.流水线操作
public class LambdaTest {
public static void main ( String [ ] args) {
List < String > list = Arrays . asList ( "apple" , "banana" , "peach" ) ;
List < String > list2 = new ArrayList < > ( ) ;
for ( String s : list) {
if ( s. startsWith ( "a" ) ) {
list2. add ( s. toUpperCase ( ) ) ;
}
}
Collections . sort ( list2) ;
List < String > list3 = list. stream ( ) . filter ( s -> s. startsWith ( "a" ) )
. map ( String :: toUpperCase )
. sorted ( )
. collect ( Collectors . toList ( ) ) ;
}
}