jav8特性特性应用
List<JSONObject> tableData = new ArrayList(1);
tableData = tableData.stream().sorted(Comparator.comparing(jsonObject -> StringNullUtil.isNotBlank((jsonObject).getString("id1Sort"))?(jsonObject).getInteger("id1Sort"):0)).collect(Collectors.toList());
List<DeptTree> collect = deptAllList.stream().filter(dept -> dept.getDeptId().intValue() != dept.getParentId())
.sorted(Comparator.comparingInt(SysDept::getSort)).map(dept -> {
DeptTree node = new DeptTree();
node.setId(dept.getDeptId());
node.setParentId(dept.getParentId());
node.setName(dept.getName());
return node;
}).collect(Collectors.toList());
Map<String, List<CatalogSystem>> map = catalogSystemsPOIList.stream().filter(item -> StrUtil.isNotBlank(item.getName()))
.collect(Collectors.groupingBy(CatalogSystem::getName));
//list按逗号隔开,输出string
List<String> msgList = new ArrayList<>();
msgList.stream().map(v->String.valueOf(v)).collect(Collectors.joining(";"))
Map<Integer, String> map = userList.stream().collect(Collectors.toMap(User::getAge, User::getName, (a, b) -> b));
Map<String, SysDept> sysDeptMap=sysDeptList.stream().collect(Collectors.toMap(SysDept::getName, v -> v));
List<SysRoleMenu> roleMenuList = Arrays.stream(menuIds.split(",")).map(menuId -> {
SysRoleMenu roleMenu = new SysRoleMenu();
roleMenu.setRoleId(roleId);
roleMenu.setMenuId(Integer.valueOf(menuId));
return roleMenu;
}).collect(Collectors.toList());
List<Integer> dataType = StringUtils.isNotBlank(quotaLibrary.getDataType()) ? JSONArray.parseArray(quotaLibrary.getDataType(), Integer.TYPE) : new ArrayList<>();
list拼接字符串逗号拼接
String join = CollectionUtil.join(deptIds, ",");
String rules = StringUtils.join(ruleList, ",");
String.join(",", newFunctionStrs)
String转list
String[] split = rules.split(",");
List<String> ruleList = Arrays.asList(split);
String[] functionStrs = StrUtil.split(f.getFieldFunction(), ",");
List<String> functionStrs = CollUtil.toList(StrUtil.split(functionField.getFieldFunction(), ","));
//list转map
List<SysDept> sysDeptList = new SysDept().selectList(sysDeptQueryWrapper);
Map<String, SysDept> sysDeptMap = new HashMap<>();
if(CollUtil.isNotEmpty(sysDeptList)){
sysDeptMap=sysDeptList.stream().collect(Collectors.toMap(SysDept::getName, v -> v));
}
Map<Long, Long> interfaceDataMap = new HashMap<>();
interfaceDataMap = interfaceData.stream().collect(Collectors.toMap(GradeInterfaceData::getInterfaceId,GradeInterfaceData::getInterfaceId));
List<TaskBaseMaterial> taskBaseMaterialList = new ArrayList<>();
newMaterialList.forEach(matMaterial -> {
TaskBaseMaterial taskBaseMaterial = new TaskBaseMaterial();
taskBaseMaterial.setTaskId(guideId);
taskBaseMaterial.setMaterialId(matMaterial.getId());
taskBaseMaterialList.add(taskBaseMaterial);
});
List<GradeInterfaceDTO> dtoList = new ArrayList<>();
list=new GradeInterface().selectList(queryWrapper);
if(list!=null&&!list.isEmpty()){
list.forEach(q -> {
//查询问题选项
GradeInterfaceDTO interfaceDTO = new GradeInterfaceDTO();
BeanUtils.copyProperties(q, interfaceDTO);
dtoList.add(interfaceDTO);
});
}
list转单个stirng数组
List<Long> oldIds = list.stream().map(QuesQuestionTask::getId).collect(Collectors.toList());
list转JSONARRAY
dnaAnswerList.stream().map(QuesAnswer::getContent).map(JSON::parseArray).toArray()
//获取最大的部门id
Integer parentId = deptAllList.stream().max(Comparator.comparing(SysDept::getParentId)).get().getParentId();
// 获取最大值
Optional<GradeQuota> userOp = gradeQuotaList.stream().max(Comparator.comparingInt(GradeQuota::getQuotaLevel));
gradeQuotaList.stream().filter(gradeQuota -> gradeQuota.getHasSon() == 0).map(GradeQuota::getId).collect(Collectors.toSet()));
tableData = tableData.stream().sorted(Comparator.comparing(jsonObject -> StringNullUtil.isNotBlank((jsonObject).getString("id1Sort"))?(jsonObject).getInteger("id1Sort"):0)).collect(Collectors.toList());
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
tableData=tableData.stream().sorted(Comparator.comparing(jsonObject -> ((JSONObject) jsonObject).getInteger("id1"))).collect(Collectors.toList());
// 查询全部部门
// List<DeptTree> deptList = deptMapper.selectList(Wrappers.emptyWrapper()).stream().filter(dept -> dept.getDeptId().intValue() != dept.getParentId())
// .sorted(Comparator.comparingInt(SysDept::getSort)).map(dept -> {
// DeptTree node = new DeptTree();
// node.setId(dept.getDeptId());
// node.setParentId(dept.getParentId());
// node.setName(dept.getName());
// return node;
// }).collect(Collectors.toList());
// 权限内部门
List<DeptTree> collect = deptAllList.stream().filter(dept -> dept.getDeptId().intValue() != dept.getParentId())
.sorted(Comparator.comparingInt(SysDept::getSort)).map(dept -> {
DeptTree node = new DeptTree();
node.setId(dept.getDeptId());
node.setParentId(dept.getParentId());
node.setName(dept.getName());
return node;
}).collect(Collectors.toList());
//java8 filter 用法
1.输出符合表达式的每一个对象
employees.stream().filter(p -> p.getAge() > 21).forEach(System.out::println);
//输出每一个对象
1
2
2.返回一个符合表达式的集合
Stream<Person> personStream = collection.stream().filter(new Predicate<Person>() {
@Override
public boolean test(Person person) {
return "男".equals(person.getGender());//只保留男性
}
});
collection = personStream.collect(Collectors.toList());//将Stream转化为List
System.out.println(collection.toString());//查看结果
//拼接
String str = Joiner.on(",").join(ids);
这种写法最简单,直接Joiner.on 拼接 “,” “#” “、”_" “-” 之类的
也是最常用的方法
//拼接
StringBuilder strBur = new StringBuilder(); list.forEach(val -> { strBur.append(val).append("#"); }); strBur.toString();
//拼接
String result = list.stream().collect(Collectors.joining("_"));
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!