Close

Java Utils - Objects.nonNull() Examples

Code Snippets Java Utils Java Java API 


Class:

java.util.Objects

java.lang.Objectjava.lang.Objectjava.util.Objectsjava.util.ObjectsLogicBig

Method:

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.


Examples


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);
}
}

Output

true




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);
}
}

Output

[a, b]




See Also