Java8 拼接 组装 去重 空指针

//拼接

String str = list.stream().map(Person::getName).collect(Collectors.joining("; "));
List<String> split= Arrays.asList(str.split(","));
forbidWord.stream().collect(Collectors.joining(","))

//组装

List<String> invoiceApplyId = invoiceDueBillInvoiceApplyIdList.stream().map(InvoiceDueBillDTO::getInvoiceApplyId).distinct().collect(Collectors.toList());

//排序

List<Student> studentList1 = students.stream()
                .sorted(Comparator.comparing((Function<Student,String>)student -> student.getUser().getId(),Comparator.nullsLast(String::compareTo)).reversed())
                .collect(Collectors.toList());

billDtoList =  billDtoList.stream().sorted(Comparator.comparing(BillDto::getDate, Comparator.nullsLast(Date::compareTo)).reversed()).collect(Collectors.toList());

//空指针 https://blog.csdn.net/W_Think/article/details/112180559

User result2 = Optional.ofNullable(user).orElseGet(() -> createNewUser());
String appId = Optional.ofNullable(autApplicationService.getAutApplicationEntity(appCode))
                .map(a -> a.getId())
                .orElseThrow(()->new BizException("应用端不存在,请传入正确的应用端code"));

//在List元素为基本数据类型及String类型时

List uniqueList = list.stream().distinct().collect(Collectors.toList());

//去重 List去重两个相同的实体类对象或者相同的单个对象字段

List<User> userList = users.stream() .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(user -> user.getName()+";"+user.getId()))), ArrayList::new));

List<ClassEntity> distinctClass = classEntities.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getProfessionId() + ";" + o.getGrade()))), ArrayList::new));
List<String> classNameList = new ArrayList(new HashSet(userList));	//用hashSet去重

//根据某个字段分组

users.stream().collect(Collectors.groupingBy(User::getName))

//stream对两个 List 遍历匹配数据的处理

List<AwardInfo> list3 = prizeRecords.stream()
                .map(map -> stockDTOList.stream()
                        .filter(m -> Objects.equals(m.getStockNo(), map.getStockNo()))
                        .findFirst().map(m -> {
                            map.setThirdStockNo(m.getThirdStockNo());
                            return map;
                        }).orElse(null))
                .filter(Objects::nonNull).collect(Collectors.toList());

// 交集

List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
intersection.parallelStream().forEach(System.out :: println);
 

// 差集 (list1 - list2)

List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());

// 并集

List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);

// 去重并集

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);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值