In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what skills to write concise java code, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Define profile information
Sometimes we put some variables in the yml configuration file for unified management.
For example
Replace @ Value with @ ConfigurationProperties
Usage
Define the entity of the corresponding field
@ Data// specifies the prefix @ ConfigurationProperties (prefix = "developer") @ Componentpublic class DeveloperProperty {private String name; private String website; private String qq; private String phoneNumber;}
Inject this bean when you use it
@ RestController@RequiredArgsConstructorpublic class PropertyController {final DeveloperProperty developerProperty; @ GetMapping ("/ property") public Object index () {return developerProperty.getName ();} 2. Replace @ Autowired with @ RequiredArgsConstructor
We all know that there are three ways to inject a bean (set injection, constructor injection, annotation injection). Spring recommends that we use a constructor to inject Bean.
Let's take a look at what the previous code looks like after it has been compiled
Provided by RequiredArgsConstructor:lombok
3. Do not return null
Counterexample
Positive example
Avoid unnecessary null pointers when calling methods elsewhere
Optional null judgment
/ / get subdirectory list public List getChild (String pid) {if (V.isEmpty (pid)) {pid = BasicDic.TEMPORARY_DIRECTORY_ROOT;} CatalogueTreeNode node = treeNodeMap.get (pid); return Optional.ofNullable (node) .map (CatalogueTreeNode::getChild) .orElse (Collections.emptyList ());} 4. If else
Not too much if else if, try strategic mode instead
5. Reduce controller business code
The business code is processed on the service layer as far as possible, and it is easy to operate and beautiful to maintain in the later stage.
Counterexample
Positive example
6. Convert an array of strings to a comma-separated string
It usually goes like this:
Public static void main (String [] args) {String strs = ""; String [] arr = new String [] {"aa", "cc", "bb"}; / / string array before conversion StringBuilder sb = new StringBuilder (); for (String ele: arr) {if (sb.length () > 0) {sb.append (",");} sb.append (ele) } strs = sb.toString (); / / comma-delimited string System.out.println (strs);}
A simpler way to write:
Public static void main (String [] args) {String [] arr = new String [] {"aa", "cc", "bb"}; / / pre-converted string array String strs = String.join (",", arr); / / comma-separated string System.out.println (strs) after conversion;} above is all the content of this article entitled "what are the techniques for writing concise java code?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.