Java Utils Java Java API
java.util.Objects
public static boolean nonNull(Object obj)
Returns true if the provided reference is non-null otherwise returns false. Useful for a java.util.function.Predicate.
true
null
false
package com.logicbig.example.objects;import java.util.Objects;public class NonNullExample { public static void main(String... args) { String str = "testString"; //equivalent to obj != null; boolean b = Objects.nonNull(str); System.out.println(b); }}
package com.logicbig.example.objects;import java.util.Arrays;import java.util.List;import java.util.Objects;import java.util.stream.Collectors;public class NonNullExample2 { public static void main(String... args) { List<String> list = Arrays.asList("a", "b", null, null); List<String> list2 = list.stream() .filter(Objects::nonNull) .collect(Collectors.toList()); System.out.println(list2); }}
[a, b]