##JAVA8新特性
##1. 接口中修饰方法为普通方法
- 普通方法 有方法体(java8 支持default,static)
- 抽象方法 没有方法体 需子类重写,再实现
示例代码:
public interface JDK8Interface {
/**
* 抽象方法 需要子类实现
* 默认类型:public、abstract
*/
void add();
/**
* default方法
*/
default void defaultGet() {
System.out.println("我是default方法");
}
static void staticGet() {
System.out.println("我是default方法");
}
}
##2.Lambda表达式
#####1.优点:
匿名内部类,简化代码;Lambda表达式匿名内部函数,简化函数的调用过程
#####2.调用定义
- 接口中只有一个抽象方法,该接口称为函数接口
- 在函数中定义Object方法(接口中)
- 使用默认default和静态方法
- @FunctionalInterface 接口为函数接口
#####3.调用方法
- 无参调用
(() ()->{}).get()
示例代码:
public class Test03 {
public static void main(String[] args) {
// 1.使用匿名内部类调用
new AcanthopanaxInterface() {
@Override
public void get() {
System.out.println("get");
}
}.get();
// 2.使用Lambda方式一
AcanthopanaxInterface acanthopanaxInterface2 = () -> {
System.out.println("lambda 0");
};
acanthopanaxInterface2.get();
// 2.使用Lambda方式二
((AcanthopanaxInterface)() -> {
System.out.println("使用Lambda方式1");
}).get();
// 3. 使用Lambda 结合内部静态方法
AcanthopanaxInterface acanthopanaxInterface = Test03::get;
acanthopanaxInterface.get();
}
private static void get() {
System.out.println("使用lamdba表达式调用方法");
}
}
- 有参调用
(1,2) -> {return i + j;}
示例代码:
public class Test04 {
public static void main(String[] args) {
//1.使用匿名内部类调用
YouShenInterface youShenInterface = new YouShenInterface() {
@Override
public String get(int i, int j) {
return i + "--" + j;
}
};
System.out.println(youShenInterface.get(1, 2));
System.out.println("=============================");
//2.使用lamdba 调用有参数函数方法 如果方法中只有一条语句,可以不需要写{}
YouShenInterface youShenInterface2 = (i, j) -> i + "--" + j;
System.out.println(youShenInterface2.get(1, 1));
// 3.使用Lambda表达式
YouShenInterface youShenInterface3 = Test04::get;
System.out.println(youShenInterface3.get(1, 2));
}
public static String get(int i, int j) {
return i + "---" + j;
}
}
##3.方法引入
#####1.作用
结合lambda表达式,让代码变得更加简洁
#####2.方式
- 静态方法引入 类名::方法名称
- 实例方法引入 new 对象实例()::方法名称
- 对象方法引入
- 构造函数引入 类名::new
代码示例:
/**
* 方法引入 需要结合lambda让代码变得更加简洁
*/
public class Test01 {
public static void main(String[] args) {
// 方式一:匿名内部类
Test01 test01 = new Test01();
MessageInterface messageInterface = (i, j) -> {
test01.objGet(i, j);
};
messageInterface.get(1, 2);
/**
* 方法引入规则:方法引入的方法返回类型 参数列表 必须要和函数接口保持一致
*/
// 方法引入
// 方式一:静态方法引入 类名::(静态)方法名称
MessageInterface messageInterface2 = Test01::staticGet;
messageInterface2.get(1, 2);
// 方式二:实例方法引入 new对象::实例方法
MessageInterface messageInterface3 = test01::objGet;
messageInterface3.get(1, 2);
}
private static void staticGet(Integer integer, Integer integer1) {
System.out.println("staticGet静态方法,传入参数必须一致");
}
public static void staticGet() {
System.out.println("staticGet");
}
public String objGet(int i, int j) {
System.out.println("objGet");
return "a";
}
}
接口代码
@FunctionalInterface
public interface MessageInterface {
void get(Integer a, Integer b);
}
构造方法代码示例
public class Test011 {
public static void main(String[] args) {
// UserInterface userInterface = () -> new UserEntity();
UserInterface UserInterface2 = UserEntity::new;;
UserInterface2.getUser();
}
}
对象方法代码示例
public class Test23 {
public static void main(String[] args) {
// 1.使用匿名内部类的形式
MayiktService mayiktService = new MayiktService() {
@Override
public String get(Test23 test23) {
return test23.objGet();
}
};
System.out.println(mayiktService.get(new Test23()));
// 2.Lambda
MayiktService mayiktService2 = (test23) -> test23.objGet();
System.out.println(mayiktService2.get(new Test23()));
// 3.方法引入 在这时候我们函数接口 第一个参数传递test23 返回调用test23.objGet方法
MayiktService mayiktService3 = Test23::objGet;
System.out.println(mayiktService3.get(new Test23()));
//Test23::objGet;----- (test23) -> test23.objGet();
// R apply(T t); T apply方法传递的参数类型 : R apply 方法返回的类型
// 需要将string类型字符串获取长度
// Function<String, Integer> strFunction = (str) -> {
// return str.length();
// };
Function<String, Integer> function2 = String::length;
System.out.println(function2.apply("mayikt"));
}
public String objGet() {
return "mayikt";
}
}
#####3.规则
方法引入 方法参数列表,返回值与函数接口参数列表,返回值必须保持一致
##4.stream流
####1.特点
新增的函数式编程最有亮点的特性,对集合进行操作,查找,过滤,排序等操作,更加高效,代码简洁
####2.示例
-
parallelStream为并行流;stream采用单线程
-
list转set stream.collect(Collectors.toSet())
-
list转map stream.collect(Collectors.toMap(UserEntity::getUserName, userEntity -> userEntity))
-
reduce求和 stream.reduce((a1, a2) -> a1 + a2).get();
-
min stream.min((o1, o2) -> o1.getAge() - o2.getAge())
-
anyMatchy 任意一个返回成功 stream.anyMatch((user) -> !“yxzx”.equals(user.getUserName()))
-
allMatch 表示都是 noneMatch相反,所有的都不是
-
过滤 stream.filter(userEntity -> !“yxzx”.equals(userEntity.getUserName()) && userEntity.getAge() > 18).forEach((userEntity -> System.out.println(userEntity.toString())));
-
sort stream.sorted((o1, o2) -> o2.getAge() - o1.getAge()).forEach((userEntity -> System.out.println(userEntity)));
-
skip 跳过,limit从头开始
##5.Optional
####1.作用
是一个可以为空的容器对象
####2.示例
- ofNullable 可以传递一个空的对象
- isPresent true 不为空 isPresent返回 false 为空
- orElse 参数为空,设定默认值
综合案例
public class Test022 {
public static void main(String[] args) {
String orderName = Test022.getOrderName();
System.out.println(orderName);
}
public static String getOrderName() {
// 优化前写法:
OrderEntity order = new OrderEntity("123456", "MAyikt");
// if (order != null) {
// String orderName = order.getOrderName();
// if (orderName != null) {
// return orderName.toLowerCase();
// }
// }
//获取我们对象中 orderNameOptional
// Optional<OrderEntity> orderOptional = Optional.ofNullable(order);
// Optional<String> orderNameOptional = orderOptional.map(orderEntity -> orderEntity.getOrderName());
// Optional<String> toLowerCase = orderNameOptional.map(name ->
// name.toLowerCase());
return Optional.ofNullable(order).map(OrderEntity::getOrderName).map(
String::toLowerCase).orElse(null);
}
}