1. 背景
- 项目开发过程中,经常需要判断list和map是否为空(==null)或者空集合(isEmpty())的问题,有时候不知道需不需无效判断,但又怕不判断变成空指针问题,因此总结一下目前遇到的问题。
2. 总结
- 通过MyBatis的Dao层查询出的list:如果DB查询结果为空,则返回的list为[]。
- 通过list转换为map:如果list为[],则返回的map为{}。
List<String> list = Lists.newArrayList();
Map<String, String> map = list.stream().collect(Collectors.toMap(l -> l, l -> l));
System.out.println(map); // {}
System.out.println(map==null); // false
System.out.println(map.isEmpty()); // true
2462

被折叠的 条评论
为什么被折叠?



