In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to judge whether Java is empty or not". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to judge whether List is empty by Java".
A question.
We have a function that returns a Panel List
Public Optional generatePanels () {... Return panels;}
At the Controller layer, if Panel List is empty, we return 404
Optional panels = generatePanels (); if (! panels.filter (panelList->! panelList.isEmpty ()). IsPresent () {throw new NotFoundError ("There is no panel")}
There are many logical places to call this function in the project, which means that there will be a lot of such duplicate code.
Solution
We can move the logic to determine whether Panel List is empty or not to the generatePanels function.
Public Optional generatePanels () {... Return panels.filter (panelList->! panelList.isEmpty ();}
In this way, there is no need to make a non-null judgment where the function is called, we can also pass the Optional directly to the framework, and it is up to the framework to decide whether to return 404.
But there is an implicit context, that is, we agree that generatePanels will return a non-empty Panel List as long as the result is returned. We need to keep this agreement in mind, otherwise we will not be able to answer the following questions.
Optional panels = generatePanels (); Panel firstPanel;if (panels.isPresent ()) {firstPanel = panels.get () .get (0); / / List may be empty, this operation will cause bug}
Of course, we can add a test to ensure that generatePanels always returns a non-empty Panel List, or we can add detailed documentation to explain the logic of this function, but people tend to forget or ignore this. Like speeding, we are always reminding people not to exceed the speed limit, and we even have laws in place, but many people still die of speeding every year.
A better plan.
For speeding, a better solution is to impose physical restrictions, such as making cars at a speed of no more than 60 km/h.
A better solution to the problem we face is to restrict it from the compiler layer to return a NonEmptyList. In this way, we don't need to remember any extra information, and the signature of this function already tells us what it will do.
Take Scala code as an example
Def option [NonEmptyList] generatePanels () {... Val panels: option [list] =. Panels.flatMap (x = > NonEmptyList.fromList (x))}
So we can safely get the first element of List.
Val panels: optionNonEmptyList [panel] = generatePanels (); var firstPanel Panel;if (panels.isSome ()) {firstPanel = panels.get (). Head;} so far, I believe you have a deeper understanding of "how Java determines whether EmptyList is empty". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.