java8常用表达式

一,将一个List中的某个属性转成一个新的List

例子:

List<QueryOrgsResp> orgs = sysOrgMapper.queryOrgsPaging(map);
List<String> orgIds=orgs.stream().map(QueryOrgsResp::getOrgId)
.collect(Collectors.toList());
还可以用Set集合,例如:
Set<String> orgIds = orgs.stream().map(QueryOrgsResp::getOrgId)
.collect(Collectors.toSet());

二,多个属性在某个List集合中进行过滤

List<SysOrg> 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<id,List>

List<SubjectRes> subjectDatas = new ArrayList<>();
Map<String, List<SubjectRes>> 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<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<String> list1 = new ArrayList<String>();
    list1.add("1");
	list1.add("2");
	list1.add("3");
	list1.add("5");
	list1.add("6");
 
    List<String> list2 = new ArrayList<String>();
    list2.add("2");
	list2.add("3");
	list2.add("7");
	list2.add("8");
 
    // 交集
    List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
    System.out.println("---交集 intersection---");
    intersection.parallelStream().forEach(System.out :: println);
 
    // 差集 (list1 - list2)
    List<String> 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<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
    System.out.println("---差集 reduce2 (list2 - list1)---");
    reduce2.parallelStream().forEach(System.out :: println);
 
    // 并集
    List<String> listAll = list1.parallelStream().collect(toList());
    List<String> listAll2 = list2.parallelStream().collect(toList());
    listAll.addAll(listAll2);
    System.out.println("---并集 listAll---");
    listAll.parallelStream().forEachOrdered(System.out :: println);
 
    // 去重并集
    List<String> 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<TeacherSimpleDto> 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<Object, Long> mapGroup = persons.stream().collect(Collectors.groupingBy(person -> person.getAge(), Collectors.counting()));
        System.out.println(mapGroup.toString());

        // 筛选Map中value大于1的key
        Stream<Object> 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<String, String> 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<String, List<ExportStudentResp>> 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) -> {
});
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值