Remove null pointer by using ObjectUtils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.collections4.CollectionUtils; | |
import org.apache.commons.lang3.ObjectUtils; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class ObjectUtilsEx { | |
public static void main(String[] args) { | |
final List<String> names = new ArrayList<>(); | |
names.add("Kunal"); | |
names.add("Akshansh"); | |
names.add(null); | |
final List<String> modifiedCollection = CollectionUtils.emptyIfNull(names) | |
.stream() | |
.filter(ObjectUtils::anyNotNull) | |
.filter( name -> name.equalsIgnoreCase("Kunal")) | |
.map(name -> name + " Sethi") | |
.collect(Collectors.toList()); | |
CollectionUtils.emptyIfNull(modifiedCollection).forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment