Java 8 check if list is null or empty using Optional
Java 8 introduced the Optionalclass to make handling of nulls less error-prone. For example, the following program to pick the lucky name has a null check as: public static final ListThis null check can be replaced with the Optional class method isPresent() as shown below: public String pickLuckyNameWIsPresent(final ListHowever, notice the writing is no easier than: return luckyName != null ? luckyName : "No lucky name found";The Optional class, however, supports other techniques that are superior to checking nulls. The above code can be re-written as below with orElse() method as below: public String pickLuckyNameWOrElse(final ListThe method orElse() is invoked with the condition "If X is null, populate X. Return X.", so that the default value can be set if the optional value is not present. There is another method called the ifPresent(Function). You can use this method to invoke an action and skip the null case completely. For example, the program below prints a message in the case, if the condition is met as: public static void pickLuckyNameOldWay(final ListThis can be re-written with ifPresent() , as shown below. in a more intuitive manner, as: public static void pickLuckyNameNewWay(final ListIf we want to throw an exception in case if no name is found, then it would be something like this: public String pickLuckyNameOldWay(final ListIt can be meaningfully replaced with orElseThrow as: public String pickLuckyNameWOrElse(final ListThere are other many more methods in the Optional class to handle null in more proper way. You can go through the Optional in Java 8 cheat sheet. As always, if you want to look into the source code for the example presented above, they are available on GitHub.
This post will discuss how to remove null values from a list using streams in Java. We have discussed how to remove null values from a list in Java using plain Java, Guava library, and Apache Commons Collections in the previous post. This post will discuss how to remove nulls from the list using streams in Java 8 and above. Java 8 introduced several enhancements to Collection interface, like removeIf() method. It removes all elements of the list that satisfy the given predicate. To remove null values from a list, we can pass Objects.nonNull() to removeIf() method:
Download Run Code Output:
2. Using Java 8We can use the Stream.filter() method that returns a stream consisting of the elements that match the given predicate. We can specify a lambda expression or method reference to remove null values from the stream, as shown below:
Download Run Code Output:
Download Run Code Output: 3. Handle null listCalling stream() and removeIf() methods on a null list will throw a NullPointerException. We can avoid that by creating an empty list (if list is null) using Optional.ofNullable(), as shown below:
Download Run Code Output: 4. Map the null values to a default valueInstead of removing null values from a list, we can replace the null values with any custom value. To illustrate, the following example replaces null values with a string.
Download Run Code Output:
Download Run Code Output: That’s all about removing null values from a List in Java.
Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding 🙂 |