JDK1.8新特性
接口的默认方法
-
接口里可以有实现的方法,方法default(真正要写出来default)关键字
-
接口里可以有实现的静态方法,在方法前面加static关键
public interface InterFace8 { //接口里面的方法如果添加了default关键字,这个方法可以在接口里面实现 //一个接口可以有多个default方法 //加default关键字,该方法必须实现 default void showName(){ System.out.println("jdk1.8的默认接口方法"); } default void showName1(){ System.out.println("jdk1.8的默认接口方法1"); } //接口加上static关键字,也必须实现 //一个接口可以有多个静态的方法 static void methodStatic(){ System.out.println("jdk的接口可以用静态实现的方法"); } }
函数式接口
接口只有一个抽象方法,可以有其他的实现方法
函数式接口一般在接口名称上添加注解 @FunctionalInterface,添加注解后会校验接口里是否只有一个抽象方法
函数式接口跟 @FunctionalInterface没有必然关系,只要符合第一条要求,不加注释他也是函数式接口
作用:函数式表达式在实现的时候可以用朗姆达lambda表达式(箭头函数)
@FunctionalInterface
public interface MyInterFace {
//一个接口如果只有一个抽象方法,这个接口就是函数式的接口
//只要满足只有一个接口方法,即便不加注解@FunctionalInterface它也是函数式接口
//注解的目的只是限制的作用
int sum(int a,int b);
default void fun1(){
}
static void fun2(){
}
}
朗姆达表达式 lambda
方法的简化写法,比如下面的方法
int sum(int a,int b){
return a + b;
}
lambda 表达式
(a, b) ->{a + b;}
lamnda表达式例子
public interface InterFace8 {
//接口里面的方法如果添加了default关键字,这个方法可以在接口里面实现
//一个接口可以有多个default方法
//加default关键字,该方法必须实现
default void showName(){
System.out.println("jdk1.8的默认接口方法");
}
default void showName1(){
System.out.println("jdk1.8的默认接口方法1");
}
//接口加上static关键字,也必须实现
//一个接口可以有多个静态的方法
static void methodStatic(){
System.out.println("jdk的接口可以用静态实现的方法");
}
}
Lambda表达式与函数式接口配合使用
- 定义函数式接口
@FunctionalInterface
public interface MathOpreation {
int opt(int a,int b);
}
-
用Lambda表达式实现接口并创建对象,调用接口方法
public static void main(String[] args) { //加法 MathOpreation add = (a, b) -> a + b; //减法 MathOpreation sub = (a, b) -> a - b; //乘法 MathOpreation multi = (a, b) -> a * b; //除法 MathOpreation div = (a, b) -> a / b; int i = add.opt(3,5); System.out.println(i); i = sub.opt(3,5); System.out.println(i); i = multi.opt(3,5); System.out.println(i); i = div.opt(3,5); System.out.println(i); }
JDK的类中,只要某个方法的类型是函数式接口,调用该方法时候就可以Lambda表达式
public static void main(String[] args) {
List<Integer> list = new ArrayList();
Collections.addAll(list,1,3,5,7,8,4);
//removeIf方法可以使用Lambda表达式,返回真元素被删除
list.removeIf(item -> item <= 2);
System.out.println(list);
List<Integer> ints = new ArrayList<>();
//遍历ist,可以用lambda表达式处理遍历出来的海个元素
list.forEach(item -> {
if(item % 2 ==0){
System.out.println(ints);
}
});
}
Stream
集合类型类都有stream方法,产生一个数据流,可以堆数据流做处理:forEach,map,filter,limt,sorted,colleect
public static void main(String[] args) {
List<String> listStr = Arrays.asList("aaa","ssss","ss","fcdswdcfa","dsacf","vfgeb","");
//filter()过滤掉不想要的内容,Lambda表达式返回false被过滤,返回true保留
//collect()把数据流重写生成一个集合,参数Collectors.toList()表示重新生成list集合并返回
//不影响原来列表的内容
List<String> listNew = listStr.stream().filter(str -> !str.isEmpty()).collect(Collectors.toList());
System.out.println(listNew);
System.out.println(listStr);
//map 映射
List<Integer> listInt = Arrays.asList(1,3,5,6,7,9);
//返回一个列表,元素是listInt元素的平方
List<Integer> list = listInt.stream().map(item -> item * item).collect(Collectors.toList());
System.out.println(list);
//limit 限制数量
Random random = new Random();
//ints()随机产生整数流,limit限制随机数个数
random.ints().limit(10).forEach(item -> System.out.println(item));
//sorted()
listInt.stream().sorted().forEach(item -> System.out.println(item));
//collect
//数据流拼接为字符串
String s = listStr.stream().collect(Collectors.joining(","));
System.out.println(s);
}
方法引用
为了解决返回值为ul的一个类
使用时先用lisPresent( )方法判断,为true再用get( )取值
public static void main(String[] args) {
Optional<Integer> sum = sum(null,6);
//如果Optional俩面有值,isPresent()返回true,没有返回false
if(sum.isPresent()){
System.out.println(sum.get());//get()从Optional里面取值
}else{
System.out.println("没有返回值");
}
}
private static Optional<Integer> sum(Integer a, Integer b){
if (a == null || b == null){
//empty()方法没有给Optional赋值
return Optional.empty();
}else{
//of(),把值封装到optional,使用的时候可以用get()方法取值
return Optional.of(a +b);
}
}
Optional
public static void main(String[] args) {
Optional<Integer> sum = sum(null,6);
//如果Optional俩面有值,isPresent()返回true,没有返回false
if(sum.isPresent()){
System.out.println(sum.get());//get()从Optional里面取值
}else{
System.out.println("没有返回值");
}
}
private static Optional<Integer> sum(Integer a, Integer b){
if (a == null || b == null){
//empty()方法没有给Optional赋值
return Optional.empty();
}else{
//of(),把值封装到optional,使用的时候可以用get()方法取值
return Optional.of(a +b);
}
}
日期时间类
LocalDate 只含年月日
LocalDataTime 包含年月日和时分秒
public static void main(String[] args) {
//创建当前时间的日期对象
LocalDateTime now = LocalDateTime.now();
System.out.println(now);//2022-08-10T14
//返回日期对象
LocalDate localDate = now.toLocalDate();
System.out.println(localDate);
//返回时间对象
LocalTime localTime = now.toLocalTime();
System.out.println(localTime);
//返回月份
int mothValue = now.getMonthValue();
System.out.println(mothValue);
//返回年份
int year = now.getYear();
System.out.println(year);
//返回一年中的第几天
int dayOfYear = now.getDayOfYear();
System.out.println(dayOfYear);
//返回几号
int dayOfMoth = now.getDayOfMonth();
System.out.println(dayOfMoth);
//返回星期几
int value = now.getDayOfWeek().getValue();
System.out.println(value);
//返回小时
int hour = now.getHour();
System.out.println(hour);
//返回分钟
int minute = now.getMinute();
System.out.println(minute);
//返回秒
int second = now.getSecond();
System.out.println(second);
//格式化日期
String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(format);
/* 日期运算的方法 */
//计算40天之后的日期
LocalDateTime localDateTime = now.plusDays(40);
System.out.println(localDateTime);
//计算30天之前的日期
LocalDateTime localDateTime1 = now.plusDays(-30);
System.out.println(localDateTime1);
}