java8常用表达式

java8常用表达式,第1张

java8常用表达式 一,将一个List中的某个属性转成一个新的List

例子:

List orgs = sysOrgMapper.queryOrgsPaging(map);
List orgIds=orgs.stream().map(QueryOrgsResp::getOrgId)
.collect(Collectors.toList());
还可以用Set集合,例如:
Set orgIds = orgs.stream().map(QueryOrgsResp::getOrgId)
.collect(Collectors.toSet());
二,多个属性在某个List集合中进行过滤
List sysOrgs = new LambdaQueryChainWrapper<>(sysOrgMapper)
        .list(); List checkStudents = students.stream()
        .filter(SchoolStudent -> SchoolStudent.getStudentName().equals(schoolStudent.getStudentName()))
        .filter(SchoolStudent -> !SchoolStudent.getStudentId().equals(schoolStudent.getStudentId()))
        .filter(SchoolStudent -> SchoolStudent.getSchoolId().equals(schoolStudent.getSchoolId()))
        .collect(Collectors.toList());
三,把一个List通过一个属性分组形成Map
List subjectDatas = new ArrayList<>();
Map> gradeMap = subjectDatas.stream().collect(Collectors.groupingBy(SubjectRes -> SubjectRes.getGradeId()));//年级分组
gradeMap.forEach((gradeId, gradeList) -> {
});
四,Guava用法总结

https://blog.csdn.net/u013278314/article/details/85100115
常用的几种:
1,集合的创建
List list =Lists.newArrayList();
List list2 = Lists.newArrayList(“a”,“b”,“c”);
Set set = Sets.newHashSet();
2,Lists.partition可以将一个大的集合分割成小集合,适用于分批查询、插入等场景。
List listStr = Lists.newArrayList(“1”, “2”, “3”,“4”,“5”,“6”,“7”);
List batchList = Lists.partition(listStr,3);
//被分割成了: [[1, 2, 3], [4, 5, 6], [7]]
3,字符串处理
3.1拼接
Joiner 可以快速地把多个字符串或字符串数组连接成为用特殊符号连接的字符串。
List list = Lists.newArrayList(“a”,“b”,“c”);
String value =Joiner.on("-").skipNulls().join(list);
System.out.println(value);
//输出为: a-b-c
3.2切割 Splitter用来分割字符串
String testString = “Monday,Tuesday,Thursday,Friday,”;
//英文分号分割;忽略空字符串
Splitter splitter = Splitter.on(",").omitEmptyStrings().trimResults();
System.out.println(splitter.split(testString).toString());
//转换为了:[Monday, Tuesday, Thursday, Friday]

五,String和List间的相互转换

1,String转list
//字符串转list
String str = “asdfghjkl”;
List lis = Arrays.asList(str.split(""));
2,list转字符串
String.join("", lis);

六,java8两个List集合取交集、并集、差集、去重并集

https://blog.csdn.net/weixin_33941350/article/details/91477207

public static void main(String[] args) {
    List list1 = new ArrayList();
    list1.add("1");
	list1.add("2");
	list1.add("3");
	list1.add("5");
	list1.add("6");
 
    List list2 = new ArrayList();
    list2.add("2");
	list2.add("3");
	list2.add("7");
	list2.add("8");
 
    // 交集
    List intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
    System.out.println("---交集 intersection---");
    intersection.parallelStream().forEach(System.out :: println);
 
    // 差集 (list1 - list2)
    List reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
    System.out.println("---差集 reduce1 (list1 - list2)---");
    reduce1.parallelStream().forEach(System.out :: println);
 
    // 差集 (list2 - list1)
    List reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
    System.out.println("---差集 reduce2 (list2 - list1)---");
    reduce2.parallelStream().forEach(System.out :: println);
 
    // 并集
    List listAll = list1.parallelStream().collect(toList());
    List listAll2 = list2.parallelStream().collect(toList());
    listAll.addAll(listAll2);
    System.out.println("---并集 listAll---");
    listAll.parallelStream().forEachOrdered(System.out :: println);
 
    // 去重并集
    List listAllDistinct = listAll.stream().distinct().collect(toList());
    System.out.println("---得到去重并集 listAllDistinct---");
    listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
 
    System.out.println("---原来的List1---");
    list1.parallelStream().forEachOrdered(System.out :: println);
    System.out.println("---原来的List2---");
    list2.parallelStream().forEachOrdered(System.out :: println);
}
七,一个List中某个属性相同进行和并,形成一个新的List
List newTeacherSimpleDtoList = new ArrayList<>();//定义新的集合
//TODO  list转换
for (TeacherSimpleDto oldTeacherSimpleDto : teachers) {
    // 遍历新的List,看是否存在,存在则拼接,不存在则放入新的List
    boolean flag = false;
    for (TeacherSimpleDto newTeacherSimpleDto : newTeacherSimpleDtoList) {
 // 新的List中存在名字相同的,存在则拼接
        if (newTeacherSimpleDto.getTeacherMobile().equals(oldTeacherSimpleDto.getTeacherMobile())) {
                  newTeacherSimpleDto.setTeacherMobile(oldTeacherSimpleDto.getTeacherMobile());
     newTeacherSimpleDto.setTeacherName(oldTeacherSimpleDto.getTeacherName());
                           
            }
            flag = true;
        }
    }
    // 新的List中不存在
    if (!flag) {
        newTeacherSimpleDtoList.add(oldTeacherSimpleDto);
    }
}
八,判断一个List中的元素值重复
   // 根据指定属性分组,并统计数量(key:指定属性,value:数量)
        Map mapGroup = persons.stream().collect(Collectors.groupingBy(person -> person.getAge(), Collectors.counting()));
        System.out.println(mapGroup.toString());

        // 筛选Map中value大于1的key
        Stream stringStream = mapGroup.entrySet().stream().filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey());
        System.out.print("重复的数据:{ ");
        stringStream.forEach(str -> {
            System.out.print(str + " ");
        });
        System.out.println("}");
 
九,解决n+1问题 
Map curStudentIdMap = db.select(qSchoolClassStudent.studentId).from(qSchoolClassStudent)
        .where(qSchoolClassStudent.classId.eq(oldbaseClassId))
        .fetch(String.class).stream().collect(Collectors.toMap(s -> s, s -> s));
curStudentIdMap.get(studentInfo.getStudentId()))
十,集合中多条件排序问题

map分组的有序排序 用linkedHashMap实现 另外List多条件排序用stream实现

Map> classMap = list.stream().sorted(Comparator.comparing(ExportStudentResp::getGradeId)
        .thenComparing(ExportStudentResp::getClassType)
        .thenComparing(ExportStudentResp::getClassCode))
        .collect(Collectors.groupingBy(ExportStudentResp -> ExportStudentResp.getClassId(), linkedHashMap::new, Collectors.toList()));//班级分组
classMap.forEach((subClassId, studentList) -> {
});

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5075103.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)