Java-java8-更多stream用法
1、map的list按字段转出map中的字段list
List<Map<String, Object>> existedMaster ;
List<String> userIdList = existedMaster.stream()
.map(y -> y.get("userid").toString())
.distinct().collect(Collectors.toList())
2、求和
List<Map<String, Object>> zhangjieList = listCourseTaskById(id);
Integer zongkeshimin = zhangjieList.stream()
.mapToInt(zhangjieMap -> MapUtil.getInt(zhangjieMap, "keshi")).sum(); //凤凰云
3、实体list 转 某个字段的list
// :: 默认已经过滤掉 为null 的情况?
List<Integer> grantClassList = courseGrantVoList.stream().map(CourseGrantVo::getClassId)
.collect(Collectors.toList());
4、实体list 中 某个字段的拼接
String classNameList = courseGrantModel.stream().map(CourseGrantModel::getCourseName)
.collect(Collectors.joining(","));
5、JSONArray转成integer list 并去空去重
public static void main(String[] args) {
//JSONArray转成integer list 并去空去重
String s = "[{\"id\":\"175\",\"name\":\"安全管理员\"},{\"id\":\"175\",\"name\":\"安全管理员\"},{\"id\":\"176\",\"name\":\"安全管理员\"},{\"name\":\"安全管理员\"}]";
List<Integer> nodeIdList = JSON.parseArray(s, JSONObject.class)
.stream().filter(jsonObject -> "role".equals(jsonObject.getString("type")))
.map(jsonObject -> jsonObject.getInteger("id")).filter(Objects::nonNull) //这是对前一步的结果进行过滤
.collect(Collectors.collectingAndThen(Collectors.toCollection(TreeSet::new), ArrayList::new));
String ss = Strings.join(nodeIdList,',');
System.out.println(nodeIdList);
}
6、String list 和 实体 list 包含判断
List<String> newAddCourseUserIdList ;
List<CourseAssociatedUser> newAddCourseUserList =
courseAssociatedUserList.stream().filter(associatedUser ->
newAddCourseUserIdList.contains(associatedUser.getUserId()))
.collect(Collectors.toList());
正文到此结束