For null check in method:
Object customObject = retrieveCustomObject(); if (Objects.isNull(customObject)) { return; }
For not null check in method:
Object anotherObject = retrieveAnotherObject(); if (Objects.nonNull(anotherObject)) { return; }
In the old fashion way for collection null check
List<Object> someObjects = methodGetList(); for (Object obj : someObjects) { if (obj == null) { continue; } doSomething(obj); }
With the Objects.nonNull method and Java8 Stream API, we can do the above in this way:
List<Object> someObjects = methodGetList(); someObjects.stream() .filter(Objects::nonNull) .forEach(this::doSomething);
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions