123456);
}
}
打印结果:
function2
function3
function1
Function 接口在 Java 中广泛用于集合框架的操作,例如 List 的 map()
方法接受一个 Function 对象作为参数,用于对集合中的每个元素进行映射转换。另外,在 Stream API 中也经常使用 Function 接口进行数据转换和处理。
示例使用 Function 接口处理集合元素:
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class FunctionExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// 使用 Function 接口处理集合元素
Function<String, Integer> nameLengthMapper = name -> name.length();
names.stream()
.map(nameLengthMapper)
.forEach(System.out::println);
}
}
理解Predicate<T>
断言类型,Predicate 接口表示一个接收一个参数并返回布尔值的函数。它常用于对集合元素进行过滤或条件判断。Predicate 接口只有一个抽象方法 test(T t)
,其中 test
方法接收一个泛型参数 T
,表示待检查的对象,然后返回一个布尔值,表示是否满足指定条件。
以下是 Predicate 接口的简单示例:
public class C305_Predicate {
/**
* Predicate<T>:这一接口定了一个默认方法:boolean test(T t),这一方法用检查给定的参数是否符合查询检查。
* Predicate<T>接口中有两中重要的方法:and(),or(),negate()。
* Predicate<T>接口中的方法如下:
* and(Predicate<? super T> other):对当前 Predicate 和另一个 Predicate 进行逻辑与操作。
* or(Predicate<? super T> other):对当前 Predicate 和另一个 Predicate 进行逻辑或操作。
* negate():返回当前 Predicate 的逻辑非。
* 下面是使用 Predicate 进行条件组合的示例:
*/
public static void main(String[] args) {
//能够被2整除
Predicate<Integer> dividedTwo = new Predicate<Integer>() {
@Override
public boolean test(Integer o) {
return o % 2 == 0;
}
};
//能够被3整除
Predicate<Integer> dividedThree = o -> o % 3 == 0;
//能够被5整除
Predicate<Integer> dividedFive = o -> o % 5 == 0;
//Predicate 支持多种逻辑操作,可以通过 and、or、negate 等方法将多个 Predicate 进行组合,形成更复杂的条件。
// and使用:true
System.out.println(dividedTwo.and(dividedThree).and(dividedFive).test(30));
// or使用:false
System.out.println(dividedTwo.and(dividedThree).or(dividedFive).test(3));
// negate使用:true
System.out.println((dividedTwo.and(dividedThree).or(dividedFive)).negate().test(3));
}
}
FunctionPredicate应用
Function作过滤函数
/**
* 过滤函数,将集合内满足一定条件的元素返回
*
* @param sourece 源数据集合
* @param predicate 处理函数Function
* @param <T>
* @return 满足断言的所有数据源集合
* TODO: 我这儿用一个List接收,我理解这不够抽象
*/
public static <T> Collection<T> filter_1(Collection<? extends T> sourece, Function<T, Boolean> predicate) {
Collection<T> temp = new ArrayList<T>();
for (T t : sourece) {
if (predicate.apply(t)) {
temp.add(t);
}
}
return Collections.unmodifiableCollection(temp);
}
断言作过滤函数
/**
* @param sourece 输入数据集合
* @param predicate 断言
* @param <E> 泛型
* @return
*/
public static <E> Collection<E> filter_2(Collection<? extends E> sourece, Predicate<E> predicate) {
Collection<E> temp = new ArrayList<E>();
for (E t : sourece) {
if (predicate.test(t)) {
temp.add(t);
}
}
return Collections.unmodifiableCollection(temp);
}
// 调用
public static void testFilter_2() {
Collection<Integer> collection = filter_2(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), (Integer i) -> {
return i % 2 == 0;
});
System.out.println(collection);
}
隐藏类型-Action
JAVA中并没有具体的Action类型,但是有一些操作有Action的作用,例如:
public class C3_06_Action {
public static void main(String[] args) {
// 匿名内置类实现
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("