JUC-10-四大函数式接口

JUC-10-四大函数式接口

新时代程序猿:jdk8

  • lambada表达式
  • 链式编程
  • 函数式接口
  • Stream流计算

1. 函数式接口简介

向runnable接口这种典型的是函数式接口

@FunctionalInterface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * @author  Arthur van Hoff
* @see java.lang.Thread
* @see java.util.concurrent.Callable
* @since JDK1.0
*/
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}

// 简化编程模型

另外可以查询JDK帮助文档:

mark

2.Function 函数式接口

java.util.Function源码:

1
2
3
4
5
6
7
8
9
10
11
@FunctionalInterface
public interface Function<T, R> {

/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
// 传入参数t,返回R类型
R apply(T t);

测试用例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.zhuuu.Function;

import java.util.function.Function;

/*
函数型接口:
有一个输入参数
有一个输出类型
可以用lambada表达式简化
*/
public class demo01 {
public static void main(String[] args) {
// 工具类
// 输出输入的值
// 方式一:
// Function<String,String> function = new Function<String,String>() {
// @Override
// public String apply(String str) {
// return str;
// }
// };

// 方式二
Function<String,String> function = (str)->{return str;};
System.out.println(function.apply("asdasd"));
}
}

3. Predicate 断定型接口

底层源码分析:

1
2
3
4
5
6
7
8
9
10
11
@FunctionalInterface
public interface Predicate<T> {

/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);

举例分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.zhuuu.Function;

/*
断定型接口:
有一个输入参数
返回值只能是布尔值
*/


import java.util.function.Predicate;

public class demo02 {
public static void main(String[] args) {
// 判断字符串是否为空
// 方式一:
// Predicate<String> predicate = new Predicate<String>() {
// @Override
// public boolean test(String str) {
// return str.isEmpty();
// }
// };
// System.out.println(predicate.test("123"));
//

// 方式二:lambada表达式
Predicate<String> predicate = (str)->{return str.isEmpty();};
System.out.println(predicate.test("123"));
}
}

4. Consumer 消费型接口

源码分析:

1
2
3
4
5
6
7
8
9
10
@FunctionalInterface
public interface Consumer<T> {

/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
// 只有输入 没有返回值
void accept(T t);

举例分析;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.zhuuu.Function;

import java.util.function.Consumer;

/*
Consumer 消费型接口:只有输入,没有返回值
*/

public class Demo03 {
public static void main(String[] args) {
// 方式一:
// Consumer<String> consumer = new Consumer<String>() {
// @Override
// public void accept(String str) {
// System.out.println(str);
// }
// };
// consumer.accept("asd");


// 方式二:
Consumer<String> consumer = (str)->{
System.out.println(str);
};
consumer.accept("asd");
}
}

5. Supplier 供给型接口

底层源码分析:

1
2
3
4
5
6
7
8
9
10
@FunctionalInterface
public interface Supplier<T> {

/**
* Gets a result.
*
* @return a result
*/
T get();
}

举例分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.zhuuu.Function;

import java.util.function.Supplier;

/*
Supplier<T>:没有参数,只有返回值
*/

public class demo04 {
public static void main(String[] args) {
// 方式一:
// Supplier<String> supplier = new Supplier<String>() {
// @Override
// public String get() {
// return "朱酱酱";
// }
// };
// System.out.println(supplier.get());

// 方式二:
Supplier<String> supplier = ()->{return "朱酱酱";};
System.out.println(supplier.get());
}
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2019-2022 Zhuuu
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信