In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
First, why do you need lazy functions
Lazy evaluation (delaying expression evaluation as much as possible) is a feature of many functional programming languages. Lazy collections provide their elements when needed without the need to pre-calculate them, which brings some benefits. First, you can postpone time-consuming calculations until they are absolutely necessary. Second, you can create unlimited collections that continue to provide elements as long as they continue to receive requests. The lazy use of functions allows you to get more efficient code. Java does not provide native support for inertia, but Scala does, making it easy to use.
2. Lazy loading is realized by java
Lazy style
Public class LazyDemo {private String property; / / attribute may also be a database connection, files and other resources public String getProperty () {if (property==null) {property=initProperty (); / / if it has not been initialized, initialize} return property;} private String initProperty () {return "property";}} III. Introduction to lazy functions
When a function is declared as lazy, the function does not execute immediately, but when we first call the function, we execute it. So we call this a lazy function (lazy loading in java).
IV. Cases
Code 1:
Object LazyDemo {def main (args: Array [String]): Unit = {val res= sum (10Magne20) / / without the modification of lazy, this function is eager's println ("=") / / println ("res:" + res)} def sum (Int= {println ("sum executed") aquib}}
Output: =
Sum has executed
Res:30
Code 2:
Object LazyDemo {def main (args: Array [String]): Unit = {lazy val res= sum (10Magne20) / / lazy modification. The sum () function is not executed immediately, but println ("=") / / println ("res:" + res)} def sum is executed after it is called for the first time: Int= {println ("sum executed")
Output: =
Code 2:
Object LazyDemo {def main (args: Array [String]): Unit = {lazy val res= sum (10Magne20) / / lazy modification. The sum () function will not be executed immediately. The println ("=") println ("res:" + res) / / call to the sum method} def sum (def sum): Int= {println ("sum executed") will not be executed immediately.
Output: =
Sum has executed
Res:30
To sum up, we can see that after being modified by lazy in scala, lazy loading can be realized, which will be very useful in the loading calculation of data in big data's project!
Five: pay attention
1) lazy cannot modify var type variables
2) after the function is modified by lazy, the operation of the function will be delayed. We are declaring a variable. If we add a lazy to the variable, then the declaration of the variable will also be delayed, and the declaration will take effect only when it is used. For example:
Scala > val astat100a: Int = 100scala > lazy val baked 1000b: Int = scala > println (b) 1000
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.