import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
Student student = new Student(1,"小明",1);
Student student1= new Student(2,"小红",2);
studentList.add(student);
studentList.add(student1);
List<Class> classList = new ArrayList<>();
Class cl = new Class(1,"1班");
Class c2 = new Class(2,"2班");
classList.add(cl);
classList.add(c2);
// list 转成map,map存放的是key和value的映射关系
Map<Integer,Class> map = new HashMap<>();
for (Class aClass : classList) {
map.put(aClass.getClassId(),aClass);
}
// Map<Integer, Class> map = classList.stream().collect(Collectors.toMap(Class::getClassId, e -> e));
List<StudentAndClass> list = new ArrayList<>();
if(studentList.size()>0){
for (Student studentArray : studentList) {
Class c1= map.get(studentArray.getClassId());
if(c1!=null){
StudentAndClass studentAndClass = new StudentAndClass();
studentAndClass.setClassId(studentArray.getClassId());
studentAndClass.setClassName(c1.getClassName());
studentAndClass.setId(studentArray.getId());
studentAndClass.setName(studentArray.getName());
list.add(studentAndClass);
}
}
}
}
多层for 循环优化方法 demo
public List<AclModuleLevelDto> roleTree(int roleId) {
// 1、当前用户已分配的权限点
List<SysAcl> userAclList = sysCoreService.getCurrentUserAclList();
// 2、当前角色分配的权限点
List<SysAcl> roleAclList = sysCoreService.getRoleAclList(roleId);
// 3、当前系统所有权限点
List<AclDto> aclDtoList = Lists.newArrayList();
Set<Integer> userAclIdSet = userAclList.stream().map(sysAcl -> sysAcl.getId()).collect(Collectors.toSet());
Set<Integer> roleAclIdSet = roleAclList.stream().map(sysAcl -> sysAcl.getId()).collect(Collectors.toSet());
List<SysAcl> allAclList = sysAclMapper.getAll();
for (SysAcl acl : allAclList) {
AclDto dto = AclDto.adapt(acl);
// 避免多才层for循环
if (userAclIdSet.contains(acl.getId())) {
dto.setHasAcl(true);
}
if (roleAclIdSet.contains(acl.getId())) {
dto.setChecked(true);
}
aclDtoList.add(dto);
}
return aclListToTree(aclDtoList);
}
软件赚钱_优米谷