In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍了Java中的惰性评估是什么及怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Java中的惰性评估是什么及怎么实现文章都会有所收获,下面我们一起来看看吧。
1、说明
惰性评估是将表达式的评估延迟到需要时才进行的过程。Java是严格的立即赋值评估。
可以使用lambda表达式和高阶函数将其重写为延迟评估的版本。
2、实例public class LazySample { public static void main(String[] args) { //这是一个lambda表达式,表现为闭包 UnaryOperator add = t -> { System.out.println("executing add"); return t + t; }; //这是一个lambda表达式,表现为闭包 UnaryOperator multiply = t -> { System.out.println("executing multiply"); return t * t; }; //传递Lambda闭包而不是普通函数 System.out.println(addOrMultiply(true, add, multiply, 4)); System.out.println(addOrMultiply(false, add, multiply, 4)); } //这是一个高阶函数 static R addOrMultiply( boolean add, Function onAdd, Function onMultiply, T t ) { // Java的?会懒惰计算表达式,因此仅执行所需的方法 return (add ? onAdd.apply(t) : onMultiply.apply(t)); }}
实例扩展:
public class SingleLock implements Lazy { private Callable codeBlock; private V value; public SingleLock(Callable codeBlock) { this.codeBlock = codeBlock; } @Override public synchronized V get() { if (value == null) { setValue(); } return value; } private void setValue() { try { value = codeBlock.call(); } catch (Exception e) { throw new RuntimeException(e); } } }关于"Java中的惰性评估是什么及怎么实现"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"Java中的惰性评估是什么及怎么实现"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.