1、Lambda表达式
1.1 概述
1.2 语法
函数式接口 引用名 = ( 参数列表) -> {
}
-> : 是一个操作符,左边写参数列表,右边写方法体
2、函数型接口
JDK常见函数型接口
Consumer
package com. sw. java2024. test2 ;
import java. util. function. Consumer ;
public class ConsumerTest {
public static void main ( String [ ] args) {
Consumer consumer = new Consumer < Integer > ( ) {
@Override
public void accept ( Integer money) {
System . out. println ( "我今天花了" + money + "大洋" ) ;
}
} ;
testConsumer ( consumer, 800 ) ;
Consumer consumer2 = ( money) -> {
System . out. println ( "我今天花了" + money + "大洋" ) ;
} ;
testConsumer ( consumer2, 1000 ) ;
Consumer consumer3 = money -> System . out. println ( "我今天花了" + money + "大洋" ) ;
testConsumer ( consumer3, 2000 ) ;
}
public static void testConsumer ( Consumer consumer, Integer money) {
consumer. accept ( money) ;
}
}
Supplier
package com. sw. java2024. test2 ;
import java. util. function. Supplier ;
public class SupplierTest {
public static void main ( String [ ] args) {
Supplier < String > supplier = new Supplier ( ) {
@Override
public String get ( ) {
return "Good" ;
}
} ;
String result = supplier. get ( ) ;
System . out. println ( result) ;
System . out. println ( testSupplier ( supplier) ) ;
System . out. println ( testSupplier ( ( ) -> { return "Hello" ; } ) ) ;
System . out. println ( testSupplier ( ( ) -> "你好" ) ) ;
}
public static String testSupplier ( Supplier < String > supplier) {
return supplier. get ( ) ;
}
}
Funtion
package com. sw. java2024. test2 ;
import java. util. function. Function ;
public class FunctionTest {
public static void main ( String [ ] args) {
Function < String , Integer > function = new Function < String , Integer > ( ) {
@Override
public Integer apply ( String str) {
try {
return Integer . valueOf ( str) ;
} catch ( NumberFormatException e) {
e. printStackTrace ( ) ;
}
return 0 ;
}
} ;
System . out. println ( function. apply ( "100" ) ) ;
System . out. println ( testFunction ( function, "1200" ) ) ;
System . out. println ( testFunction ( ( str) -> {
try {
return Integer . valueOf ( str) ;
} catch ( NumberFormatException e) {
e. printStackTrace ( ) ;
}
return 0 ;
} , "2000" ) ) ;
System . out. println ( testFunction ( ( str) -> Integer . valueOf ( str) , "5555" ) ) ;
}
public static Integer testFunction ( Function < String , Integer > function, String numStr) {
return function. apply ( numStr) ;
}
}
Predicate
package com. sw. java2024. test2 ;
import java. util. function. Predicate ;
public class PredicateTest {
public static void main ( String [ ] args) {
Predicate < String > predicate = new Predicate < String > ( ) {
@Override
public boolean test ( String name) {
if ( name. startsWith ( "zhang" ) ) {
return true ;
}
return false ;
}
} ;
System . out. println ( predicate. test ( "zhangsan" ) ) ;
boolean result = testPredicate ( predicate, "lisi" ) ;
System . out. println ( result) ;
result = testPredicate ( ( name) -> {
if ( name. startsWith ( "zhang" ) ) {
return true ;
}
return false ;
} , "zhangwuji" ) ;
System . out. println ( result) ;
result = testPredicate ( ( name) -> {
return name. startsWith ( "zhang" ) ;
} , "zhangsanfeng" ) ;
System . out. println ( result) ;
result = testPredicate ( ( name) -> name. startsWith ( "zhang" ) , "zhangcuishan" ) ;
System . out. println ( result) ;
}
public static boolean testPredicate ( Predicate < String > predicate, String name ) {
return predicate. test ( name) ;
}
}
3、方法引用
3.1 概述
3.2 前提条件
当Lambda表达式中只是调用了其他方法,并且被调用的方法已经存在了,就可以使用方法引用
3.3 方式
对象::实例方法 类::静态方法 类::实例方法 类::new
3.4 案例
package com. sw. java2024. test2 ;
import java. io. PrintStream ;
import java. util. Comparator ;
import java. util. function. Consumer ;
import java. util. function. Function ;
import java. util. function. Supplier ;
public class MethodReferenceTest {
public static void main ( String [ ] args) {
Consumer con1 = ( name) -> {
System . out. println ( name) ;
} ;
Comparator < Integer > com1 = new Comparator < Integer > ( ) {
@Override
public int compare ( Integer o1, Integer o2) {
return Integer . compare ( o1, o2) ;
}
} ;
Comparator < Integer > com2 = ( o1, o2) -> Integer . compare ( o1, o2) ;
Comparator < Integer > com3 = Integer :: compare ;
System . out. println ( com3. compare ( 10 , 20 ) ) ;
System . out. println ( com3. compare ( 10 , 10 ) ) ;
System . out. println ( com3. compare ( 30 , 20 ) ) ;
Function < Person , String > fun1 = new Function < Person , String > ( ) {
@Override
public String apply ( Person person) {
return person. getPersonName ( ) ;
}
} ;
Function < Person , String > fun2 = ( person) -> person. getPersonName ( ) ;
Function < Person , String > fun3 = Person :: getPersonName ;
System . out. println ( fun3. apply ( new Person ( 10 , "张三" ) ) ) ;
Supplier < Person > sup1 = new Supplier < Person > ( ) {
@Override
public Person get ( ) {
return new Person ( ) ;
}
} ;
Supplier < Person > sup2 = ( ) -> new Person ( ) ;
Supplier < Person > sup3 = Person :: new ;
Person person = sup3. get ( ) ;
System . out. println ( person) ;
}
}
4、Stream流
一般从集合中筛选出新的数据时,就会选用
4.1 概述
4.2 特点
本身不存储数据 不会改变源对象,操作后会返回一个新Stream对象 延迟操作,在需要结果时才会执行。
4.3 获取Stream流
package com. sw. java2024. test3 ;
import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. List ;
import java. util. Random ;
import java. util. function. Supplier ;
import java. util. function. UnaryOperator ;
import java. util. stream. IntStream ;
import java. util. stream. Stream ;
public class StreamTest {
public static void main ( String [ ] args) {
List < String > list = new ArrayList < > ( ) ;
list. add ( "zhangwuji" ) ;
list. add ( "zhangsan" ) ;
list. add ( "zhangcuishan" ) ;
list. add ( "zhangxiaoxiao" ) ;
list. add ( "zhangdada" ) ;
list. add ( "lisi" ) ;
list. add ( "zhangsanfeng" ) ;
Stream < String > stream1 = list. stream ( ) ;
stream1. forEach ( System . out:: println ) ;
Stream < String > stream2 = list. parallelStream ( ) ;
stream2. forEach ( System . out:: println ) ;
Integer [ ] nums = { 10 , 20 , 30 , 20 , 10 , 25 , 12 , 22 } ;
Stream < Integer > stream3 = Arrays . stream ( nums) ;
stream3. forEach ( System . out:: println ) ;
Stream < Integer > stream4 = Stream . of ( 100 , 200 , 100 , 210 , 250 ) ;
stream4. forEach ( System . out:: println ) ;
Stream < Integer > stream5 = Stream . iterate ( 100 , ( n) -> n * 2 ) ;
stream5 = stream5. limit ( 5 ) ;
stream5. forEach ( System . out:: println ) ;
Stream < Integer > stream6 = Stream . generate ( ( ) -> new Random ( ) . nextInt ( 100 ) ) ;
stream6. limit ( 10 ) . forEach ( System . out:: println ) ;
System . out. println ( "----------------------" ) ;
IntStream intStream = IntStream . of ( 100 , 200 , 100 , 210 , 250 ) ;
intStream. forEach ( System . out:: println ) ;
IntStream range = IntStream . range ( 10 , 20 ) ;
range. forEach ( System . out:: println ) ;
IntStream rangeClosed = IntStream . rangeClosed ( 10 , 20 ) ;
rangeClosed. forEach ( System . out:: println ) ;
}
}
4.4 API操作
package com. sw. java2024. test3 ;
import com. sw. java2024. test2. Person ;
import java. util. ArrayList ;
import java. util. Comparator ;
import java. util. List ;
import java. util. Optional ;
import java. util. function. BiFunction ;
import java. util. function. BinaryOperator ;
import java. util. function. Function ;
import java. util. stream. Collectors ;
import java. util. stream. Stream ;
public class StreamTest2 {
public static void main ( String [ ] args) {
List < Person > persons = new ArrayList < > ( ) ;
persons. add ( new Person ( 10 , "小强10" ) ) ;
persons. add ( new Person ( 11 , "小强11" ) ) ;
persons. add ( new Person ( 12 , "小强12" ) ) ;
persons. add ( new Person ( 15 , "大强15" ) ) ;
persons. add ( new Person ( 2 , "小小2" ) ) ;
persons. add ( new Person ( 1 , "小强1" ) ) ;
Stream < Person > stream = persons. stream ( ) ;
stream
. filter ( ( person) -> person. getPersonName ( ) . startsWith ( "小" ) )
. distinct ( )
. sorted ( new Comparator < Person > ( ) {
@Override
public int compare ( Person o1, Person o2) {
return o1. getPersonId ( ) - o2. getPersonId ( ) ;
}
} )
. map ( Person :: getPersonName )
. parallel ( )
. forEach ( System . out:: println ) ;
Optional < Person > min = stream. min ( ( p1, p2) -> p1. getPersonId ( ) - p2. getPersonId ( ) ) ;
Person person = min. get ( ) ;
System . out. println ( person) ;
stream = persons. stream ( ) ;
Optional < Person > max = stream. max ( ( p1, p2) -> p1. getPersonId ( ) - p2. getPersonId ( ) ) ;
System . out. println ( max. get ( ) ) ;
stream = persons. stream ( ) ;
long count = stream. count ( ) ;
System . out. println ( count) ;
stream = persons. stream ( ) ;
List < Person > list = stream. limit ( 3 ) . collect ( Collectors . toList ( ) ) ;
System . out. println ( list) ;
stream = persons. stream ( ) ;
Optional < Integer > reduce = stream. map ( Person :: getPersonId ) . reduce ( ( x, y) -> x + y) ;
System . out. println ( reduce. get ( ) ) ;
}
}
5、新时间API
5.1 本地化日期时间
类库
LocalDate:年月日 LocalTime:时分秒 LocalDateTime:年月日 时分秒 API
now:获取当前时间对象 ------- 静态方法 plusXxx:增加 ------- 普通方法 minXxx:减少 ------- 普通方法
package com. sw. java2024. test4 ;
import java. time. LocalDateTime ;
public class NewDateTest {
public static void main ( String [ ] args) {
LocalDateTime ldt = LocalDateTime . now ( ) ;
int year = ldt. getYear ( ) ;
int month = ldt. getMonthValue ( ) ;
int day = ldt. getDayOfMonth ( ) ;
System . out. println ( year + "-" + month + "-" + day) ;
LocalDateTime ldt1 = ldt. plusWeeks ( 6 ) ;
System . out. println ( ldt1) ;
LocalDateTime ldt2 = ldt. minusDays ( 2 ) ;
System . out. println ( ldt2) ;
}
}
5.2 时间戳
类库
API
now:获取当前时间戳对象 getEpochSecond:获取毫秒值
package com. sw. java2024. test4 ;
import java. time. Instant ;
public class InstantTest {
public static void main ( String [ ] args) {
Instant instant = Instant . now ( ) ;
System . out. println ( instant) ;
System . out. println ( instant. getEpochSecond ( ) ) ;
}
}
5.3 时区
类库
API
systemDefault ------- 静态方法
package com. sw. java2024. test4 ;
import java. time. ZoneId ;
import java. util. Set ;
public class ZoneIdTest {
public static void main ( String [ ] args) {
ZoneId zoneId = ZoneId . systemDefault ( ) ;
System . out. println ( zoneId. toString ( ) ) ;
System . out. println ( "-----------" ) ;
Set < String > zoneIds = ZoneId . getAvailableZoneIds ( ) ;
System . out. println ( zoneIds. size ( ) ) ;
for ( String id : zoneIds) {
System . out. println ( id) ;
}
}
}
5.4 新时间之间相互转换
Date —>Instant
date.toInstant() ------- 普通方法 Instant---->LocalDateTime
LocalDateTime.ofInstant(instant, zoneId) ------- 静态方法 LocalDateTime —>Instant
localDateTime.atZone(zoneId).toInstant() ------- 普通方法 Instant---->Date
Date.from(instant) ------- 静态方法
package com. sw. java2024. test4 ;
import java. time. Instant ;
import java. time. LocalDate ;
import java. time. LocalDateTime ;
import java. time. ZoneId ;
import java. util. Date ;
public class DateChangeTest {
public static void main ( String [ ] args) {
Date date = new Date ( ) ;
long time = date. getTime ( ) ;
System . out. println ( time) ;
Instant instant = date. toInstant ( ) ;
System . out. println ( instant. getEpochSecond ( ) ) ;
LocalDateTime ldt = LocalDateTime . ofInstant ( instant, ZoneId . systemDefault ( ) ) ;
System . out. println ( ldt) ;
LocalDateTime ldt1 = LocalDateTime . now ( ) ;
Instant i1 = ldt1. atZone ( ZoneId . systemDefault ( ) ) . toInstant ( ) ;
System . out. println ( i1) ;
Date date1 = Date . from ( i1) ;
System . out. println ( date1) ;
}
}
5.5 时间格式化类
DateTimeFormatter
使用的是 LocalDateTime 的方法
localDateTime.ofPattern:时间转字符串 ------- 普通方法 LocalDateTime.parse:字符串转时间 ------- 静态方法
package com. sw. java2024. test4 ;
import java. time. LocalDate ;
import java. time. LocalDateTime ;
import java. time. format. DateTimeFormatter ;
import java. time. temporal. TemporalAccessor ;
import java. util. Date ;
public class DateTimeFormatterTest {
public static void main ( String [ ] args) {
DateTimeFormatter df = DateTimeFormatter . ofPattern ( "yyyy-MM-dd HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime . now ( ) ;
String dateStr = ldt. format ( df) ;
System . out. println ( dateStr) ;
LocalDateTime localDateTime = LocalDateTime . parse ( "2001-12-12 05:12:12" , df) ;
System . out. println ( localDateTime) ;
}
}