1 Star 0 Fork 1

LiuYan/Java

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
Part02.java 1.75 KB
Copy Edit Raw Blame History
liuyan authored 2023-05-10 13:21 +08:00 . java9、java10、java11
package functionalInterface;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* @author LiuYan
* @date 2021/11/17 15:08
* <p>
* Java8 内置的四大核心函数式接口
* <p>
* Consumer<T>:消费型接口
* void accept(T t);
* <p>
* Supplier<T>:供给型接口
* T get();
* <p>
* Function<T,R>:函数型接口
* R apply(T t);
* <p>
* Predicate<T>:断言型接口
* boolean test(T t);
*/
public class Part02 {
public static void main(String[] args) {
happy(100, x -> System.out.println("今天消费" + x));
System.out.println(getNumList(10, () -> (int) (Math.random() * 100)));
System.out.println(strHandler("\t\t\t 学习Java", str -> str.trim()));
System.out.println(filterStr(Arrays.asList("123", "6789","7878787"), str -> str.length() > 3));
}
private static void happy(double money, Consumer<Double> consumer) {
consumer.accept(money);
}
private static List<Integer> getNumList(int num, Supplier<Integer> supplier) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add(supplier.get());
}
return list;
}
private static String strHandler(String str, Function<String, String> function) {
return function.apply(str);
}
private static List<String> filterStr(List<String> strList, Predicate<String> predicate) {
ArrayList<String> list = new ArrayList<>();
for (String s : strList
) {
if (predicate.test(s)) {
list.add(s);
}
}
return list;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Liu-Yan-Code/java.git
git@gitee.com:Liu-Yan-Code/java.git
Liu-Yan-Code
java
Java
master

Search