不要再用if(xxx != null)或者try catch NullPointerException了,Optional可以帮你解决
2017-12-18 16:56
561 查看
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)); }
相关文章推荐
- Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPointerException 错误的集中原因及解决办法
- java 8 Optional 解决NullPointerException
- Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPointerException 错误的集中原因及解决办法
- Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- org.apache.jasper.JasperException: java.lang.NullPointerException 解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- java 出现NullPointerException的原因及解决办法
- Android中利用NetworkInfo判断网络状态时出现空指针(NullPointerException)问题解决
- 解决Context.getExternalFilesDir(null)返回null造成java.lang.NullPointerException的问题
- Java Compiler API 发生java.lang.NullPointerException异常解决办法
- Java自定义一维数组、二维数组的实例化,解决一维数组、二维数组NullPointerException异常
- atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException v2 q31
- Android NullPointerException解决方法
- android开发中遇到的问题解决之RelativeLayout执行measure(w,h)方法时,出现NullPointerException的异常情况
- java.lang.NullPointerException:Attempt to invoke virtual method xxx on a null object reference
- Java异常解决之--java.lang.NullPointerException
- paip.c3p0 数据库连接池 NullPointerException 的解决...
- Exception starting filter struts2 java.lang.NullPointerException 解决...
- Eclipse Errors running builder 'CDT Builder' java.lang.NullPointerException解决办法