增加函数式编程
This commit is contained in:
parent
40061235a4
commit
90e13cc1f2
37
src/main/java/com/zeekling/function/FunctionTest.java
Normal file
37
src/main/java/com/zeekling/function/FunctionTest.java
Normal file
@ -0,0 +1,37 @@
|
||||
package com.zeekling.function;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zeekling
|
||||
* @apiNote 函数式编程
|
||||
* @since 2021-07-04
|
||||
*/
|
||||
public class FunctionTest {
|
||||
|
||||
public static <T> List<T> filter(List<T> list, Predicate<T> p) {
|
||||
List<T> results = new ArrayList<>();
|
||||
for(T s: list){
|
||||
if(p.test(s)){
|
||||
results.add(s);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> listOfStrings = new ArrayList<String>(){{
|
||||
add("");
|
||||
add("1");
|
||||
add("2");
|
||||
}};
|
||||
Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
|
||||
List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
|
||||
System.out.println(Arrays.toString(nonEmpty.toArray()));
|
||||
|
||||
}
|
||||
|
||||
}
|
18
src/main/java/com/zeekling/function/Predicate.java
Normal file
18
src/main/java/com/zeekling/function/Predicate.java
Normal file
@ -0,0 +1,18 @@
|
||||
package com.zeekling.function;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zeekling
|
||||
* @apiNote 函数式编程测试
|
||||
* @since 2021-07-04
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Predicate<T> {
|
||||
|
||||
boolean test(T t);
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user