[Java] I want to check that the elements in the list are null or empty [Collection Utils]

Recently, I've been doing only source reviews, but since there were a lot of children who are doing their best to check the null / empty list, I thought, "Maybe I don't know it?", So I summarized it.

Answer 1) How to do your best

The "work hard check" that I often pointed out in recent reviews looks like this: arrow_down:

Work hard


List<String> strList = new ArrayList<String>();
//Assuming strList is defined somewhere or comes in as a method argument ...

if (strList == null || strList.size() == 0) {
  return true;
}

Yeah, it's right. If you can check null & size, you can avoid nullPointerException which is boring for the time being. For new employee training from April to June, if you can write this way, you will pass: white_flower:

Answer 2) Smart way

Java has a lot of useful external libraries (mostly programming languages, not just Java). The famous doco is "org.apache.commons". This time I will write smartly using org.apache.commons.

Smart processing


import org.apache.commons.collections4.*

List<String> strList = new ArrayList<String>();
//Assuming strList is defined somewhere or comes in as a method argument ...

if (CollectionUtils.isEmpty(strList)) {
  return true;
}

Notable is in the if statement: exclamation: I used "Apache Commons Collections" which is one of the external libraries.

Apache Commons Collections is a library that handles Java collections conveniently, and there is `CollectionUtils.isEmpty ()` that handles null check and size 0 check together!

Caution!

Since it is an external file, it cannot be used just by installing Java normally: cry: You need to download and load it.

In eclipse, if ```org.apache.commons.collections4. * `` `appears in the import candidates when input completion of CollectionUtils, it can be judged that it has been read. However, it is mostly introduced in most Java projects, maybe.

Summary

Null / empty check of List can be written neatly by using `CollectionUtils.isEmpty ()`: pencil2: However, it must be pre-loaded with Apache Commons Collections.

Recommended Posts

[Java] I want to check that the elements in the list are null or empty [Collection Utils]
I want to simplify the conditional if-else statement in Java
[Java] I want to perform distinct with the key in the object
Glassfish tuning list that I want to keep for the time being
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
I want to get the IP address when connecting to Wi-Fi in Java
I want to send an email in Java.
rsync4j --I want to touch rsync in Java.
I want to get the value in Ruby
10 barrages of drawing with ● or ■ that are likely to appear in training (Java)
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
[Java] (list == null || list.size () == 0) I don't like checking NULL / empty
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
# 1_JAVA I want to get the index number by specifying one character in the character string.
I tried to implement the Euclidean algorithm in Java
In Java, I want to trim multiple specified characters from only the beginning and end.
[CQ Engine] I want to handle collections like Stream or .Net LINQ even in Java 7.
I want to set the conditions to be displayed in collection_check_boxes
Easy way to check method / field list in Java REPL
I want to make a list with kotlin and java!
Identify threads in the Java process that are wasting CPU
I want to use the Java 8 DateTime API slowly (now)
Difference between element 0, null and empty string (check in list)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to recursively search the class list under the package
I want to transition to the same screen in the saved state
[Java] How to search for a value in an array (or list) with the contains method
I implemented the code to learn multiple images at once in the Watson Visual Recognition Collection in Java.
I tried to summarize the words that I often see in docker-compose.yml
I want to return to the previous screen with kotlin and java!
The story of Collectors.groupingBy that I want to keep for posterity
I want to display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
Determine if the strings to be compared are the same in Java
I want to change the value of Attribute in Selenium of Ruby
[Android] I want to get the listener from the button in ListView
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
I want to write JSP in Emacs more easily than the default.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I called the COTOHA API parser 100 times in Java to measure performance.
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
I want to display an error message when registering in the database
Process to check if "some fields are duplicated" from the Bean List
I want to ForEach an array with a Lambda expression in Java
[Ruby] I tried to summarize the methods that frequently appear in paiza
I want to get the field name of the [Java] field. (Old tale tone)
[Java Spring MVC] I want to use DI in my own class
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
Android development, how to check null in the value of JSON object
[Rails] I want to reset everything because the data in the local environment is strange! What to do before that
Java reference to understand in the figure
Change List <Optional <T >> to Optional <List <T >> in Java
How to get the date in java
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I went to the Java Women's Club # 1
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)