公司设计网站有哪些,市场管理监督局是干什么的,网站如何做服务器授权,卸载西部数码网站管理助手目录
一、介绍
二、示例
#xff08;一#xff09;Consumer
源码解析
测试示例
#xff08;二#xff09;Comparator
#xff08;三#xff09;Predicate
三、应用
四、总结 一、介绍 FunctionalInterface是一种信息注解类型#xff0c;用于指明接口类型声明…目录
一、介绍
二、示例
一Consumer
源码解析
测试示例
二Comparator
三Predicate
三、应用
四、总结 一、介绍 FunctionalInterface是一种信息注解类型用于指明接口类型声明成为Java语言规范定义的函数式接口。从概念上说函数式接口只有一个抽象方法因为默认方法有一个实现所以他们不是抽象的。如果一个接口声明了一个抽象方法覆盖java.lang的一个公共方法这也不计入接口的抽象方法计数因为接口的任何实现都将有来自java.lang.Object或其他地方的实现。函数式接口的实例可以使用lambda表达式、方法引用或构造函数引用来创建。 二、示例
一ConsumerT
消费者表示一个接受单个输入参数并且不返回结果的操作。
源码解析
accept方法接收一个参数并对该参数执行特定的操作没有返回值
addThen方法接受一个consumer类型的对象它将一个consumer对象与另一个consumer对象进行关联该方法会返回一个新的consumer对象它首先执行当前consumer的accept方法然后再执行传入的after consumer对象的accpet方法。
FunctionalInterface
public interface ConsumerT {/*** Performs this operation on the given argument.** param t the input argument*/void accept(T t);/*** Returns a composed {code Consumer} that performs, in sequence, this* operation followed by the {code after} operation. If performing either* operation throws an exception, it is relayed to the caller of the* composed operation. If performing this operation throws an exception,* the {code after} operation will not be performed.** param after the operation to perform after this operation* return a composed {code Consumer} that performs in sequence this* operation followed by the {code after} operation* throws NullPointerException if {code after} is null*/default ConsumerT andThen(Consumer? super T after) {Objects.requireNonNull(after);return (T t) - { accept(t); after.accept(t); };}
}
测试示例
public class ConsumerTest {public static void main(String[] args) {ConsumerString printUpperCase s - System.out.println(s.toUpperCase());ConsumerString printLength s - System.out.println(s.length());ConsumerString combine printUpperCase.andThen(printLength);ListString names Arrays.asList(Alice, Bob, Charlie, David, Jim);names.forEach(printUpperCase);names.forEach(combine);}
}
二ComparatorT
比较器compare方法是Comparator接口中的方法它用于比较两个对象的大小。一般来说如果第一个对象小于第二个对象则返回负整数如果第一个对象等于第二个对象则返回零如果第一个对象大于第二个对象则返回正整数。
public class ComparatorTest {public static void main(String[] args) {// 自定义比较器实现compare方法比较规则是自然数降序排列CustomedComparator customedComparator new CustomedComparator();ListInteger list Arrays.asList(5, 8, -2, 0, 10);list.sort(customedComparator);// forEach函数传入一个consumer对象底层是加强for循环 调用accpetlist.forEach(ele - System.out.println(ele));}
}
Comparator接口声明了函数式接口但接口中声明了两个抽象方法这显然不符合之前给的定义。首先我先验证是否注解允许多个抽象方法验证得出声明此注解的接口只能有一个抽象方法。Comparator接口中声明了equals和compare两个抽象方法 其中equals是Object类的公共方法这里令我不解的是接口中equals方法是声明的抽象方法但它却无需实现这里需要注意一下。最后声明函数式接口只有一个抽象方法这是肯定的。
三PredicateT
断言predicateT代表了一个接受一个参数并返回布尔值结果的判断条件。该接口中只有一个抽象方法test用于对给定的参数进行判断并返回一个布尔值。
public class PredicateTest {public static void main(String[] args) {PredicateInteger predicate num - num % 2 0;System.out.println(predicate.test(11));System.out.println(predicate.test(0));}
}
三、应用 forEach方法迭代器方法参数是consumer对象。Arrays.sort()方法传入comparator对象自定义比较Stream流操作lambda表达式 四、总结 函数式接口是JDK8的新特性在函数式接口使用ambda表达式会使代码更加简洁上述内容如果有错误的地方希望大佬们可以指正。我一直在学习的路上您的帮助使我收获更大觉得对您有帮助的话还请点赞支持我也会不断更新文章