You can using java-8 Stream#anyMatch to checking the strings is whether null or empty. for example:
boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
.anyMatch(it-> it==null || it.isEmpty());
OR you can replace lambda expression with method reference expression which the method can be reused later:
boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
.anyMatch(this::isNullOrEmptyString);
boolean isNullOrEmptyString(String it){
return it==null || it.isEmpty();
}
OR as I see your question that marked as spring, and spring already has a utility method StringUtils#isEmpty for check a String whether is null or empty:
boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
.anyMatch(StringUtils::isEmpty);
Answer from holi-java on Stack OverflowYou can using java-8 Stream#anyMatch to checking the strings is whether null or empty. for example:
boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
.anyMatch(it-> it==null || it.isEmpty());
OR you can replace lambda expression with method reference expression which the method can be reused later:
boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
.anyMatch(this::isNullOrEmptyString);
boolean isNullOrEmptyString(String it){
return it==null || it.isEmpty();
}
OR as I see your question that marked as spring, and spring already has a utility method StringUtils#isEmpty for check a String whether is null or empty:
boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
.anyMatch(StringUtils::isEmpty);
I would suggest Apache StringUtils class : https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isAnyEmpty-java.lang.CharSequence...-
Videos
You can't do it directly, you should provide your own way to check this. Eg.
class MyClass {
Object attr1, attr2, attr3;
public boolean isValid() {
return attr1 != null && attr2 != null && attr3 != null;
}
}
Or make all fields final and initialize them in constructors so that you can be sure that everything is initialized.
import org.apache.commons.lang3.ObjectUtils;
if(ObjectUtils.isEmpty(yourObject)){
//your block here
}
Another non-reflective solution for Java 8, in the line of paxdiabo's answer but without using a series of if's, would be to stream all fields and check for nullness:
return Stream.of(id, name)
.allMatch(Objects::isNull);
This remains quite easy to maintain while avoiding the reflection hammer.
Try something like this:
public boolean checkNull() throws IllegalAccessException {
for (Field f : getClass().getDeclaredFields())
if (f.get(this) != null)
return false;
return true;
}
Although it would probably be better to check each variable if at all feasible.
The easiest way to check is entity == null. There is no shorter way to do that.
Note that there is a method for this in the standard lib:
Objects.isNull(Object obj)
And another one which is the opposite of the above one:
Objects.nonNull(Object obj)
And there is yet another one which can be used to force a value to be not null, it throws a NullPointerException otherwise:
T Objects.requireNonNull(T obj);
Note: The Objects class was added in Java 7, but the isNull() and nonNull() methods were added only in Java 8.
try this using reflection
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Objects;
public class ObjectsUtils {
public static boolean allNull(Object target) {
return Arrays.stream(target.getClass()
.getDeclaredFields())
.peek(f -> f.setAccessible(true))
.map(f -> getFieldValue(f, target))
.allMatch(Objects::isNull);
}
private static Object getFieldValue(Field field, Object target) {
try {
return field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
What about isEmpty() ?
if(str != null && !str.isEmpty())
Be sure to use the parts of && in this order, because java will not proceed to evaluate the second part if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty() if str is null.
Beware, it's only available since Java SE 1.6. You have to check str.length() == 0 on previous versions.
To ignore whitespace as well:
if(str != null && !str.trim().isEmpty())
(since Java 11 str.trim().isEmpty() can be reduced to str.isBlank() which will also test for other Unicode white spaces)
Wrapped in a handy function:
public static boolean empty( final String s ) {
// Null-safe, short-circuit evaluation.
return s == null || s.trim().isEmpty();
}
Becomes:
if( !empty( str ) )
Use org.apache.commons.lang.StringUtils
I like to use Apache commons-lang for these kinds of things, and especially the StringUtils utility class:
import org.apache.commons.lang.StringUtils;
if (StringUtils.isNotBlank(str)) {
...
}
if (StringUtils.isBlank(str)) {
...
}
Useful method from Apache Commons:
org.apache.commons.lang.StringUtils.isBlank(String str)
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isBlank(java.lang.String)
To detect if a string is null or empty, you can use the following without including any external dependencies on your project and still keeping your code simple/clean:
if(myString==null || myString.isEmpty()){
//do something
}
or if blank spaces need to be detected as well:
if(myString==null || myString.trim().isEmpty()){
//do something
}
you could easily wrap these into utility methods to be more concise since these are very common checks to make:
public final class StringUtils{
private StringUtils() { }
public static bool isNullOrEmpty(string s){
if(s==null || s.isEmpty()){
return true;
}
return false;
}
public static bool isNullOrWhiteSpace(string s){
if(s==null || s.trim().isEmpty()){
return true;
}
return false;
}
}
and then call these methods via:
if(StringUtils.isNullOrEmpty(myString)){...}
and
if(StringUtils.isNullOrWhiteSpace(myString)){...}