当前位置: 首页 > news >正文

网站制作哪家公司好写软文平台

网站制作哪家公司好,写软文平台,做电影网站怎么降低内存,非凡网站建设 新三板Function 我们知道Java8的最大特性就是函数式接口。所有标注了FunctionalInterface注解的接口都是函数式接口,具体来说,所有标注了该注解的接口都将能用在lambda表达式上。 接口介绍 /*** Represents a function that accepts one argument and produces…

Function

我们知道Java8的最大特性就是函数式接口。所有标注了@FunctionalInterface注解的接口都是函数式接口,具体来说,所有标注了该注解的接口都将能用在lambda表达式上。

接口介绍

/*** Represents a function that accepts one argument and produces a result.** <p>This is a <a href="package-summary.html">functional interface</a>* whose functional method is {@link #apply(Object)}.** @param <T> the type of the input to the function* @param <R> the type of the result of the function** @since 1.8*/

上述描述可知: Function中传递的两个泛型:T,R分别代表 输入参数类型和返回参数类型。下面将逐个介绍Function中的各个接口:

接口1: 执行具体内容接口
R apply(T t);

实例1:apply使用
    // 匿名类的方式实现Function<Integer, Integer> version1 = new Function<Integer, Integer>() {@Overridepublic Integer apply(Integer integer) {return integer++;}};int result1 = version1.apply(20);// lamda表达式Function<Integer, Integer> version2 = integer -> integer++;int result2 = version1.apply(20);

接口2: compose
该方法是一个默认方法,这个方法接收一个function作为参数,将参数function执行的结果作为参数给调用的function,以此来实现两个function组合的功能。

// compose 方法源码
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));}
实例2:compose使用
public int compute(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {return function1.compose(function2).apply(a);
}// 调用上述方法
test.compute(2, value -> value * 3, value -> value * value) 
// 执行结果: 12 (有源码可以看出先执行before)

接口3 : andThen
了解了compose方法,我们再来看andThen方法就好理解了,听名字就是“接下来”,andThen方法也是接收一个function作为参数,与compse不同的是,先执行本身的apply方法,将执行的结果作为参数给参数中的function。

public interface Function<T, R> {default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));}
}
实例3:andThen使用
public int compute2(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {return function1.andThen(function2).apply(a);
}// 调用上述方法
test.compute2(2, value -> value * 3, value -> value * value) 
// 执行结果 : 36

反思: 多个参数

Function接口虽然很简洁,但是由Function源码可以看出,他只能传一个参数,实际使用中肯定不能满足需求。下面提供几种思路:

  1. BiFunction可以传递两个参数(Java8中还提供了其它相似Function)
  2. 通过封装类来解决
  3. void函数还是无法解决

因为参数原因“自带的Function”函数必然不能满足业务上复杂多变的需求,那么就自定义Function接口吧

@FunctionalInterfacestatic interface ThiConsumer<T,U,W>{void accept(T t, U u, W w);default ThiConsumer<T,U,W> andThen(ThiConsumer<? super T,? super U,? super W> consumer){return (t, u, w)->{accept(t, u, w);consumer.accept(t, u, w);};}}

自此,Function接口介绍完毕。

断言性接口:Predicate

接口介绍:

/*** Represents a predicate (boolean-valued function) of one argument.** <p>This is a <a href="package-summary.html">functional interface</a>* whose functional method is {@link #test(Object)}.** @param <T> the type of the input to the predicate** @since 1.8*/
@FunctionalInterface
public interface Predicate<T> {

Predicate是个断言式接口其参数是<T,boolean>,也就是给一个参数T,返回boolean类型的结果。跟Function一样,Predicate的具体实现也是根据传入的lambda表达式来决定的。

源码不再具体分析,主要有 test/and/or/negate方法,以及一个静态方法isEqual,具体使用实例如下:

    private static void testPredict() {int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};List<Integer> list = new ArrayList<>();for (int i : numbers) {list.add(i);}// 三个判断Predicate<Integer> p1 = i -> i > 5;Predicate<Integer> p2 = i -> i < 20;Predicate<Integer> p3 = i -> i % 2 == 0;List test = list.stream().filter(p1.and(p2)
//                        .and(Predicate.isEqual(8)).and(p3)).collect(Collectors.toList());System.out.println(test.toString());//print:[6, 8, 10, 12, 14]}

供给性接口:Supplier

接口介绍

/*** Represents a supplier of results.** <p>There is no requirement that a new or distinct result be returned each* time the supplier is invoked.** <p>This is a <a href="package-summary.html">functional interface</a>* whose functional method is {@link #get()}.** @param <T> the type of results supplied by this supplier** @since 1.8*/
@FunctionalInterface
public interface Supplier<T> 

使用实例:

        Supplier supplier = "Hello"::toLowerCase;System.out.println(supplier);

消费性:Consumer

接口介绍

/*** Represents an operation that accepts a single input argument and returns no* result. Unlike most other functional interfaces, {@code Consumer} is expected* to operate via side-effects.** <p>This is a <a href="package-summary.html">functional interface</a>* whose functional method is {@link #accept(Object)}.** @param <T> the type of the input to the operation** @since 1.8*/
@FunctionalInterface
public interface Consumer<T> {

实际使用

    NameInfo info = new NameInfo("abc", 123);Consumer<NameInfo> consumer = t -> {String infoString = t.name + t.age;System.out.println("consumer process:" + infoString);};consumer.accept(info);

总结:
本文主要介绍Java8的接口式编程,以及jdk中提供的四种函数接口(FunctionalInterface)。Predict/Supplier/Consumer其实是Function的一种变形,所以没有详细介绍。
疑问: FunctionalInterface注解是如何和lamada表达式联系在一起,函数接口在编译时又是如何处理的?后面再了解下

转载于:https://www.cnblogs.com/NeilZhang/p/11086698.html

http://www.lbrq.cn/news/2542015.html

相关文章:

  • 网站开发预算编制常见的搜索引擎
  • 靓号注册网站免费seo推广优化公司哪家好
  • 做网站还是微信小程序百度24小时人工电话
  • 北京网站设计公司哪家公司好互联网
  • wordpress+3.2.1漏洞seo优化技巧有哪些
  • 学校门户网站功能外贸平台排名
  • 咖啡网站建设市场分析一个新产品策划方案
  • 网站地图怎样做绍兴seo公司
  • flash网站设计欣赏seo是什么服
  • 怎样修改网站的主页内容个人如何在百度做广告
  • 网站设计公司合肥网站推广属于哪些
  • 怎么自己做网站制作网站的基本步骤
  • 西安高校网站建设网站友链查询接口
  • 义乌百度推广公司杭州百度快照优化排名
  • 网站怎么样做采集别人网站的文章seowhy教研室
  • 有没有人通过网站建设卖东西的如何制作一个自己的网站
  • 合肥网站建设合肥做网站seo网站快速排名软件
  • 滕州网站建设网站行吗推广文章的步骤
  • 做网站的后台开发需要会些什么求职seo
  • php网站标题修改谷歌浏览器下载安装2022最新版
  • 做任务挣钱网站平台营销策略都有哪些
  • 青岛做网站的公司哪个比较好seo效果分析
  • 商城网站建站系统网络推广图片
  • 工程建设信息都在哪个网站发布服装品牌策划及营销推广方案
  • wordpress加入海报功能福州短视频seo机会
  • 徐州网络建站模板站长号
  • 做网站的画布是多少怎么制作网站链接
  • 怎么做网站关键词视频今日头条郑州头条新闻
  • 云南软件开发百度seo快排软件
  • 公司网站首页导航html专业百度seo排名优化
  • 【Python修仙编程】(二) Python3灵源初探(11)
  • [论文阅读] 人工智能 + 软件工程 | GitHub Marketplace中CI Actions的功能冗余与演化规律研究
  • 网络安全基础知识【6】
  • eSIM技术深度解析:从物理芯片到数字革命
  • Linux性能监控与调优全攻略
  • 【暑期每日一题】洛谷 P9390 金盏花