I need an assistance on one of the optional concepts of java 8+. I have been seeing developers using Optional.ofNullable on list object which is incorrect and I am not sure how its been working perfectly without bugs/defect. Code goes this way
Note: list object is being captured by making DB call from repository layer.
Optional.ofNullable(list) .orElse(Collections.emptyList()) .stream().forEach(x-> { ------ ------};);
List cannot be literal null if there is no elements it would be empty([]) so, how can it handle Optional.ofNullable() correctly? I tried dummy code to test by adding print statement in forEach It would print in both cases(list empty as well not empty).