您的位置:首页 > 运维架构

不要再用if(xxx != null)或者try catch NullPointerException了,Optional可以帮你解决

2017-12-18 16:56 519 查看
public static void testIfPresent() {
Map<String, Map<String, String>> map = new HashMap<>();

String firstKey = "hjz", secondKey = "lxk";

Map<String, String> childMap = new HashMap<>();
childMap.put("lxk", "sbj");

map.put("hjz", childMap);

ifPresent(map, firstKey, secondKey, System.out::print);
}

public static void ifPresent(Map<String, Map<String, String>> map, String firstKey, String secondKey, Consumer<String> consumer) {
ofNullable(map.get(firstKey)).ifPresent(childMap -> ofNullable(childMap.get(secondKey)).ifPresent(consumer));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐