java - Generic Method to Get Optional Item from Map -
in java 8, i'm trying define generic method whether map<k, v>
contains k key
:
private static optional<v> find(final map<k, v> map, final k key) { final v result = map.get(key); return (result == null) ? optional.empty() : optional.of(key); }
but bunch of compile-time errors on k
, v
:
[error] ... compilation failure: [error] ~/app.java:[31,47] cannot find symbol [error] symbol: class k [error] location: class com.myapp.app.app [error] ~/app.java:[31,50] cannot find symbol [error] symbol: class v [error] location: class com.myapp.app.app
how can resolve these errors?
private static <k, v> optional<v> find(final map<k, v> map, final k key) { final v result = map.get(key); return optional.ofnullable(result); }
credit - sotirios delimanolis mentioning option#ofnullable
, failure list generic parameters.
Comments
Post a Comment