1.接口中default方法
package com.jdk8new.defau;
public interface Car1 {
default void print(){
System.out.println("我是BMW");
}
}
package com.jdk8new.defau;
public interface Car2 {
default void print(){
System.out.println("我是红旗");
}
}
package com.jdk8new.defau;
public class CarImpl implements Car1,Car2 {
@Override
public void print() {
Car1.super.print();
}
}
2.jdk中的函数式接口
package com.jdk8new.function;
import java.util.function.*;
public class FunctionTest {
public static void main(String[] args) {
//接收两个参数,没有返回值
testBiConsumer();
System.out.println("---end BiConsumer------");
//接收两个参数,返回一个参数
testBiFunction();
//BiFunction的一种特殊情况,两个相同的入参,一个出参与入参类型一致
testBinaryOperation();
//返回boolean值,接收两个参数(例如比较两个值的大小)
testBiPredicate();
//没有入参,返回boolean值(例如比较两个字符串大小)
testBooleanSupplier("1","1");
//接收一个参数,没有返回值,适合对对象进行加工
testConsumer();
}
private static void testConsumer() {
Consumer<User> reSetFun = u -> u.setId(u.getName());
User u = new User();
u.setId("1");
u.setName("dadfa");
reSetFun.accept(u);
System.out.println(u.getId());
}
private static void testBooleanSupplier(String a,String b) {
BooleanSupplier su = ()-> a.equals(b);
}
private static void testBiPredicate() {
//定义一个比较大小的函数,a如果比b大返回true否则返回false
BiPredicate<Integer,Integer> compareFun=(a,b)-> a>b;
System.out.println(compareFun.test(1,2));
}
private static void testBinaryOperation() {
BinaryOperator<User> us1=(u1,u2)-> u1;
us1.apply(new User(),new User());
}
private static void testBiFunction() {
//此函数输入两个object入参,返回一个object出参
//自定义实现逻辑两数相加
BiFunction<Integer,Integer,Integer> addMath=(a,b)-> a+b;
//此函数接收一个Object入参,返回一个Object出参
//自定义实现了对入参增加2倍,并返回
Function<Integer,Integer> then = a->a*2;
//此方法的执行逻辑是,先执行1+2 =3,再对3执行乘以2=6
int x=addMath.andThen(then).apply(1,2);
System.out.println(x);
}
private static void testBiConsumer() {
//接收两个Object参数,没有返回值
BiConsumer<Integer,Integer> bb= (a, b)-> System.out.println(a+b);
//andThen的意思再一次调用
bb.andThen(bb).andThen(bb).accept(1,2);
}
}
package com.jdk8new.function;
public class User {
private String name;
private String id;
public User(){
super();
}
public User(String id, String name){
this.id=id;
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "{id:"+id+"--name:"+name+"}";
}
}
3.jdk8中lambda表达式
package com.jdk8new.lamda;
@FunctionalInterface
public interface IFuncInterface {
public int add(int a ,int b);
}
package com.jdk8new.lamda;
public class LambdaTest {
public static void main(String[] args) {
IFuncInterface addMath = (a,b)->a+b;
IFuncInterface subMath =(a,b)-> a-b;
System.out.println(addMath.add(1,2));
}
}
4.jdk8中方法引用
package com.jdk8new.methodref;
public interface Supp<T> {
T get();
}
package com.jdk8new.methodref;
public class Car {
public static Car create(Supp<Car> supp){
return supp.get();
}
public static void collide(final Car car){
System.out.println("colide:"+car.toString());
}
public void repar(){
System.out.println("repar:"+this.toString());
}
public void other(Car otherCar){
System.out.println("other:"+otherCar.toString());
}
}
package com.jdk8new.methodref;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainTest {
public static void main(String[] args) {
//等价与下面的语法
Car car1=Car.create(new Supp<Car>() {
@Override
public Car get() {
return new Car();
}
});
//等价于
Car car2=Car.create(()->new Car());
//等价于
Car car3 = Car.create(Car::new);
List<Car> cars = Arrays.asList(car1,car2,car3);
//静态方法调用
cars.forEach(Car::collide);
//特定类的任意对象的方法
cars.forEach(Car::repar);
//特定对象的方法引用
Car car4=Car.create(Car::new);
cars.forEach(car4::other);
List<String> list= new ArrayList<>();
list.add("sdfsf1");
list.add("sdfsf2");
list.add("sdfsf3");
list.add("sdfsf4");
list.add("sdfsf5");
list.forEach(System.out::println);
}
}
5.jdk8中stream流
package com.jdk8new.methodref;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainTest {
public static void main(String[] args) {
//等价与下面的语法
Car car1=Car.create(new Supp<Car>() {
@Override
public Car get() {
return new Car();
}
});
//等价于
Car car2=Car.create(()->new Car());
//等价于
Car car3 = Car.create(Car::new);
List<Car> cars = Arrays.asList(car1,car2,car3);
//静态方法调用
cars.forEach(Car::collide);
//特定类的任意对象的方法
cars.forEach(Car::repar);
//特定对象的方法引用
Car car4=Car.create(Car::new);
cars.forEach(car4::other);
List<String> list= new ArrayList<>();
list.add("sdfsf1");
list.add("sdfsf2");
list.add("sdfsf3");
list.add("sdfsf4");
list.add("sdfsf5");
list.forEach(System.out::println);
}
}
6.jdk8中optional类
package com.jdk8new.optional;
import java.util.Optional;
public class TestOptional {
public static void main(String[] args) {
testEmpty();
}
private static void testEmpty() {
//创建一个空的Optional对象
Optional<Object> empty = Optional.empty();
//创建一个值为Integer=10的Optional对象
Optional<Integer> pi = Optional.of(10);
Optional<Integer> p2 = Optional.of(10);
//判断optional对象中是否存在值
System.out.println(empty.isPresent());
//Optional对象中如果有值,调用ifPresent方法定义的函数式接口自定义逻辑
pi.ifPresent(System.out::println);
//创建一个值可以为null的Optional对象
Optional<Integer> inter = Optional.ofNullable(11);
inter.ifPresent(System.out::println);
//获取Optional对象中的值,如果不存在,返回参数other
Integer integer = pi.orElse(12);
System.out.println(integer);
Object o = empty.orElse(13);
System.out.println(o);
//获取Optional对象中的值,如果不存在,返回接口自定义逻辑中的值
Object o1 = empty.orElseGet(() -> 10);
System.out.println(o1);
System.out.println(pi.equals(p2));
}
}
网友评论