Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple null checks in Java 8

I have the below code which is bit ugly for multiple null checks.

String s = null;  if (str1 != null) {     s = str1; } else if (str2 != null) {     s = str2; } else if (str3 != null) {     s = str3; } else {     s = str4; } 

So I tried using Optional.ofNullable like below, but its still difficult to understand if someone reads my code. what is the best approach to do that in Java 8.

String s = Optional.ofNullable(str1)                    .orElse(Optional.ofNullable(str2)                                    .orElse(Optional.ofNullable(str3)                                                    .orElse(str4))); 

In Java 9, we can use Optional.ofNullablewith OR, But in Java8 is there any other approach ?

like image 741
sparker Avatar asked Feb 21 '19 06:02

sparker


People also ask

How do I stop null check in Java 8?

Java 8 introduced an Optional class which is a nicer way to avoid NullPointerExceptions. You can use Optional to encapsulate the potential null values and pass or return it safely without worrying about the exception. Without Optional, when a method signature has return type of certain object.

How do you handle null values in Java 8?

With the release of Java 8, a straight-forward way of handling null references was provided in the form of a new class: java. util. Optional<T> . It's a thin wrapper around other objects, so you can interact with the Optional instead of a null reference directly if the contained object is not present.

Can list have multiple null values Java?

From the documentation : Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1. equals(e2), and they typically allow multiple null elements if they allow null elements at all.

How to get rid of null checks in Java 8?

We can get rid of all those null checks by utilizing the Java 8 Optional type. The method map accepts a lambda expression of type Function and automatically wraps each function result into an Optional. That enables us to pipe multiple map operations in a row.

Is it easier to check nulls with optional class in Java?

However, notice the writing is no easier than: 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:

How to get rid of NullPointerException in Java 8?

We have to write a bunch of null checks to make sure not to raise a NullPointerException: We can get rid of all those null checks by utilizing the Java 8 Optional type. The method map accepts a lambda expression of type Function and automatically wraps each function result into an Optional.

How do you check if a parameter is null in Java?

Here, we can use Java Assertions instead of the traditional null check conditional statement: public void accept(Object param) { assert param != null ; doSomething (param); } In line 2, we check for a null parameter. If the assertions are enabled, this would result in an AssertionError.


1 Answers

You may do it like so:

String s = Stream.of(str1, str2, str3)     .filter(Objects::nonNull)     .findFirst()     .orElse(str4); 
like image 142
Ravindra Ranwala Avatar answered Sep 20 '22 03:09

Ravindra Ranwala