简体   繁体   中英

What if null is passed to apply method of Function Interface in java?

userMap is a hashmap which contains userId as key.

userDtoTransformer.apply(userMap.get(userId));

What if there is no such userId present in the map? Then apply method will have null as argument. So, will it throw an exception?

apply method just calls your function with the passed argument. So your code would be equal to methodRepresentedByUserDtoTransformer(userMap.get(userId)) . So it behaves identical to this invocation, so if you don't have the user in your map then you would have the equivalent of call methodRepresentedByUserDtoTransformer(null) , and then it depends on how your method handles null arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM