加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JDK8新特性

发布时间:2020-12-15 08:01:03 所属栏目:Java 来源:网络整理
导读:JDK8新特性 1、FunctionInterface 是什么?和Lambda表达式有什么关系? package com.wood.functional ; /** * @Description: 函数式接口 * @Author wood * @Date 2019-08-15 * * @FunctionalInterface 声明一个接口是函数式接口,目的是可以用lambda表达式的
JDK8新特性

1、FunctionInterface 是什么?和Lambda表达式有什么关系?

package com.wood.functional;

/**
* @Description: 函数式接口
* @Author wood
* @Date 2019-08-15
*
* @FunctionalInterface 声明一个接口是函数式接口,目的是可以用lambda表达式的方式实现接口
* 函数式接口只能包含一个抽象方法,重写父类的方法不算
* Java中所有的对象都是继承自Object,所以MyInterface实际上是继承自Object,所以有toString()
*
* 在将函数作为一等公民的语言(JavaScript、Python)中,Lambda表达式(匿名函数、闭包)是函数,
* 但在Java中,Lambda表达式是对象(Java中一切皆对象),必须依附于一类特别的对象FunctionalInterface
*
* JDK8强调传递行为,而不仅仅是数据
*
*/
@FunctionalInterface
public interface MyInterface {

// 函数式接口只能包含一个抽象方法
void test();

// 重写父类(Object)的方法不算,所以能够编译通过
String toString();

}
package com.wood.functional;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @Description: 测试函数式接口
* @Author wood
* @Date 2019-08-15
*/
public class MyTest {

public void testInterface(MyInterface myInterface) {
myInterface.test();
}

public static void main(String[] args) {
MyTest myTest = new MyTest();
myTest.testInterface(new MyInterface() {
@Override
public void test() {
System.out.println("调用函数式接口test():通过new MyInterface()的方式");
}
});

myTest.testInterface(() -> System.out.println("调用函数式接口test():通过lambda表达式的方式"));

MyInterface myInterface = () -> {
System.out.println("这是MyInterface的实现,是一个匿名函数");
};
myTest.testInterface(myInterface);

}

}


2、FunctionInterface函数式接口强调传递的是行为,不是数据。

 
package com.wood.functional;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @Description: 测试函数式接口
* @Author wood
* @Date 2019-08-15
*/
public class MyTest {

public void testInterface(MyInterface myInterface) {
myInterface.test();
}

public static void main(String[] args) {
MyTest myTest = new MyTest();
myTest.testInterface(new MyInterface() {
@Override
public void test() {
System.out.println("调用函数式接口test():通过new MyInterface()的方式");
}
});

myTest.testInterface(() -> System.out.println("调用函数式接口test():通过lambda表达式的方式"));

MyInterface myInterface = () -> {
System.out.println("这是MyInterface的实现,是一个匿名函数");
};
myTest.testInterface(myInterface);


List<Integer> list = Arrays.asList(1,2,3,4,5,6);
/**
* JDK8开始,接口可以有默认实现,用default关键字修饰
* default void forEach(Consumer<? super T> action) {
* Objects.requireNonNull(action);
* for (T t : this) {
* action.accept(t);
* }
* }
* */
list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println(integer);
}
});

// 方法引用
list.forEach(System.out::println);


/**
* JDK8强调传递行为,而不仅仅是数据
* */
System.out.println("-----JDK8强调传递行为,而不仅仅是数据-----");
System.out.println(myTest.calculate(2,t -> 2 * t));
System.out.println(myTest.calculate(2,t -> t * t));
System.out.println(myTest.calculate(2,t -> t + 1));

System.out.println("-----compose-----(先执行t -> t + 1 再执行t -> 2 * t)");
System.out.println(myTest.compose(2,t -> 2 * t,t -> t + 1)); // 6

System.out.println("-----andThen-----(先执行t -> 2 * t 再执行t -> t + 1)");
System.out.println(myTest.andThen(2,t -> t + 1)); // 5

System.out.println("-----BiFunction是Function的增强形式-----");
/**
* 如果是JDK7传统的做法是,需要写4个方法(加、减、乘、除)
* 而JDK8可以借助于BiFunction来完成,传递的是行为
* */
System.out.println(myTest.biFunction(1,(t1,t2) -> t1 + t2));
System.out.println(myTest.biFunction(1,t2) -> t1 - t2));
System.out.println(myTest.biFunction(1,t2) -> t1 * t2));
System.out.println(myTest.biFunction(1,t2) -> t1 / t2));

System.out.println("-----biFunction andThen-----(先执行(t1,t2) -> t1 + t2 再执行t -> t * t)");
System.out.println(myTest.biFunctionAndThen(1,t2) -> t1 + t2,t -> t * t)); // 9

// stream获取流并对流操作,操作完成后再将它转换为集合
System.out.println(list.stream().filter(t -> t > 2).collect(Collectors.toList()));
// 原来的集合不变,流操作返回的是新集合
System.out.println(list);
System.out.println(myTest.biFunctionFilter(3,list,(value,integerList) -> integerList.stream().filter(t -> t > value).collect(Collectors.toList())));

}

/**
* 计算
* 高阶函数:如何一个方法接收一个函数作为参数或返回值是一个函数
* @param t 输入值
* @param function 对t执行的行为函数,Function<Integer(输入类型),Integer(输出类型)>
* @return 操作后的值
*/
public int calculate(int t,Function<Integer,Integer> function) {
return function.apply(t);
}

public int compose(int t,Integer> function1,Integer> function2) {
// 先执行function2的行为,再执行function1的行为
return function1.compose(function2).apply(t);
}

public int andThen(int t,Integer> function2) {
// function1 -> function2
return function1.andThen(function2).apply(t);
}

public int biFunction(int t1,int t2,BiFunction<Integer,Integer,Integer> biFunction) {
// BiFunction<A,B,Integer> biFunction 对入参A、B执行biFunction行为返回Integer类型的值
// Function是一个输入,BiFunction是两个输入
return biFunction.apply(t1,t2);
}

public int biFunctionAndThen(int t1,Integer> biFunction,Integer> function) {
// andThen表示先执行biFunction,再执行function
return biFunction.andThen(function).apply(t1,t2);
}

public List<Integer> biFunctionFilter(int t,List<Integer> list,List<Integer>,List<Integer>> biFunction) {
return biFunction.apply(t,list);}}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读