JDK1.8新增特性

新特性:

Lambda表达式: (语法三要素:参数、箭头、代码)

JDK1.8引入的一种新语法Lambda表达式,它简化了匿名内部类的使用和提高代码的可读性。

java 复制代码
/**正常写法创建Runable**/
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        System.out.println("*************");
    }
};

/**Lambda表达式写法*/
Runnable runnable = () -> System.out.println("*************");

接口提供默认方法和静态方法:

java 复制代码
public interface UserService {
    /**
     * 默认方法
     */
    default void add(){
        System.out.println("add");
    };
   
    /**
     * 静态方法
     * @param id
     * @return
     */

    public static User getUser(String id){
        return new User();
    };
}

Stream API :

JDK1.8引入的一种新抽象流,提供一种新的处理数据方式,对集合或数据元素进行函数式编程数据处理;所有 Collection 集合都可以通过 stream 默认方法获取流。

java 复制代码
  List<String> list = Arrays.asList("ASDSFSDSF","NBDS","WKEKRERE","BC8D");
            
  //通过stream流过滤列表中长度等4的字符串列表
  List<String> newList = list.stream().filter(pre->pre.length()==4).collect(Collectors.toList());
  //通过stream流把列表内容转换小写并返回一个新的列表
  List<String> toLowerCaseList = list.stream().map(String::toLowerCase).collect(Collectors.toList());
  //获取集合长度
  long count = list.stream().count();
  //获取集合第1个元素
  Optional<String> first = list.stream().findFirst();

函数试接口 :

1、自定义函数试接口

java 复制代码
@FunctionalInterface
public interface MyFunctionalInterface {
    int add(int a,int b);
}
//调用
MyFunctionalInterface myFunctionalInterface = (a,b)-> a+b;
int a = myFunctionalInterface.add(5,2);
System.out.println(a);

2、Predicate<T>:判断型接口输入一个参数T,返回一个布尔值

java 复制代码
 Predicate<String> isEmpty = p -> p.isEmpty();
 // 输出: false
 System.out.println(isEmpty.test("abc"));

3、Function<T,R>:函数试接口有输入参数T和返回结果R,用于执行一些转换逻辑。

java 复制代码
Function<Integer, Integer> function = (Integer i) -> i * i;
// 输出: 100
System.out.println(function.apply(10));

4、Supplier<T>:供给型接口,无参数返回数据类型T。

5、Consumer<T>:消费型接口,有参数T,无返回值。

java 复制代码
public class User {
    public void follow(final String str) {
        System.out.println("Following the " + str);
    }
}

Consumer<String> follow = user :: follow;
follow.accept("Consumer....");

方法引用 :

1、构造器引用: ClassName::new

2、类的静态方法引用: ClassName::staticMethodName

3、对象的实例方法引用: object::methodName

4、特定类型的实例方法引用:TypeName::methodName

LocalDate API :

1、LocalDate : 年月日;

2、LocalTime : 时分秒;

3、LocalDateTime : 年月日时分秒;

4、Period : 两个日期之间的时间间隔;

5、Duration : 两个时间之间的持续时间;

相关推荐
Chester_19997 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲8 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员9 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导9 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan9 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251499 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
2601_961901709 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..10 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
大衛說10 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
QQ_216962909611 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端