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/01 Report--
Most people do not understand the knowledge points of this "Java code writing case analysis" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java code writing case analysis" article.
IntStream.range (1,5) .boxed () .map (I-> {System.out.print ("Happy Birthday"); if (I = = 3) return "dear NAME"; else return "to You";}) .forEach (System.out::println)
Although I have 10 years of experience in Java programming, to be honest, I am ashamed that this piece of code is like a brick slapped on my head. I have learned Lambda expressions, I have learned Stream streams, and I have also learned the forEach () method, but I still have a bit of indigestion when I put all these knowledge points into a single line of code.
I always thought it would be better to write this line of code in the old-fashioned syntax (before Java 7), like this:
For (int I = 1; I
< 5; i++) { System.out.println("Happy Birthday " + (i == 3 ? "dear NAME" : "to you")); } 哇,beautiful code,简洁又明了!不仅我能看得懂,就连刚入门的 Java 小白都能看得懂。要知道,代码是写给人看的,假如只有自己看得懂,只有自己觉得很 6,而其他人看起来云里雾里的,就不一定是好代码,尽管好像是行走在技术的前沿。 俗话说得好,"能力越大,责任越大"。Java 8 为我们提供了强大的能力,它的新特性是划时代的(Java 8 之后的版本中新特性都不够亮眼),包括 Lambda 表达式和 Stream 流,我们通过它们可以写出简洁又高效的代码。打个不恰当的比喻,Java 8 之前,开发者驾驶的是桑塔纳,Java 8 之后,开发者驾驶的是法拉利。 但如果驾驶技术不好的话,法拉利也能变成桑塔纳,甚至还不如。拿之前那段看起来似乎很 6 的代码来说吧,它并不是一段好的代码--尽管使用了新颖的技术,但难以理解。 在 Java 8 之前,如果你想编写函数式代码的话,应该会使用 Google 的 Guava 类库,它是一个很棒的开源类库(不陌生吧),可以在一定程度上弥补 Java 原生类库的不足。我在它的 wiki 上看到下面这样一条建议,说得很富有远见: Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable and inefficient code. .. when you go to preposterous lengths to make your code "a one-line", the Guava team weeps. 应该能看得懂吧?大致的意思就是说,如果过度使用 Guava 的函数式编程的话,会导致代码冗长、混乱、不可读,甚至低效;如果有些开发者为了减少代码的长度,刻意把多行代码"优化"成一行代码时,Guava 甚至会被玩哭。 我只能说,优秀的人真可怕,他不仅知道自己的长处,更了解自己的不足--说的就是你,Guava 的开发者。至于开头提到的那位大牛,他写的代码我就不敢恭维,只能说炫技炫到盲目自信吧。根据我的经验,只有很少一部分的大牛能够保持理智,在追求技术创新的同时意识到炫技的问题。 我认为,Guava wiki 上的建议同样适用于 Java 8,好技术要妥善的利用,而不是滥用。众所周知,Java 8 的新特性可以用来减少冗余代码,当我们把一个复杂的匿名内部类变成一个简洁的 Lambda 表达式就是一个很好的例子。 Thread t1 = new Thread(new Runnable() { @Override public void run() { System.out.println("匿名内部类,搞起来"); } }); t1.start(); // 优化后 Thread t2 = new Thread(() ->{
System.out.println ("Lambda expression, do it")
});
T2.start (); the above is about the content of the article "case Analysis of Java Code Writing". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
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.