In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "what are the tips for using Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Using stream to realize list to map
Normal: for list to map, we often use traversal, and then take out the value and put it in the result, as follows
Magic trick: implemented with stream, through Collectors.groupingBy
Stream has many very intuitive and useful methods, such as sum,count,distinct and so on.
Reduce non-null judgment and use Objects tools gracefully
Usually use! = null to make judgment, which is less elegant.
Jdk1.7 provides Objects tools, which are intuitive and easy to use. Many methods help us to make a non-empty judgment and reduce the code of! = null.
Random numbers can use seeds to ensure random rules.
Sometimes in order to save a fixed value, we often need to save a database or file, which is very troublesome. We can calculate a specific value through the seed of a random number.
For example, the values of the five cycles from the second print must be equal. Just make sure that the value of seed is fixed, and the value calculated by random number must be fixed.
Double curly braces syntax
In many cases, you need to initialize some list data. If you do something like the following, it will be tedious.
Magic trick: use the {{}} syntax, as follows
Note: many students mentioned that there is a problem here, it is not recommended to use a lot of programs like this, because this method is an anonymous inner class initialization method, it will produce a lot of xxx$1.class,xxx$2.class. There will be a memory leak problem.
Goto to be used in special scenarios
It is well known that java does not have goto syntax, but it provides methods similar to goto syntax effects, as follows
Try-with-resource automatically shuts down resources
JVM cannot automatically collect references to external resources, such as database connections, network connections, input and output IO streams, and so on. These connections need to be closed manually, otherwise it will lead to external resource leakage, connection pool overflow and abnormal occupation of files.
Therefore, in the IO operation, we often need to write the following code to manually release resources
Jdk1.7 provides us with try-with-resource syntax, so that we no longer have to call the close method in finally, jvm automatically calls.
Use MultiValueMap to build Map
If you need to build Map
This is the end of the content of "what are the tips for using Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.