回答

收藏

关于使用Lambda 表达式,根据List中的对象的某个属性,求两个List的差集,

技术问答 技术问答 685 人阅读 | 0 人回复 | 2023-09-11

一、需求逻辑:
: E- b- p8 t8 w, z+ }' q- A1.先取出所有的车辆信息listWithoutDuplicates,并去重(该List数据最全,元素均为CarVo);
2 \; l. b4 Z/ {6 b2.再从另一个表中取出车辆信息carForAdminList;(数据较少,元素均为CarForAdmin);( G8 f- w2 h! G: o6 N, B; m, V
3.根据carForAdminList中的CarForAdmin车牌号是否存在于listWithoutDuplicates中为条件(即CarForAdmin.车牌号等于CarVo.车牌号),得出listWithoutDuplicates中多出的元素,并形成一个newTransferList* D2 J! Y# @( z7 ]" U5 k# M. k
二、问题:
+ O- H  r6 C" a( r" I% E使用以下代码进行取差集:
    0 X6 r+ c2 }, B) ]
  • List<CarVo> listWithoutDuplicates = CarDao.pageVoList(param);, u3 v3 R6 i1 k
  • List<CarForAdmin> carForAdminList = CarDao.pageList(param);: g1 A3 p' b  G4 G, C  z; r
  • List<CarVo> newTransferList = listWithoutDuplicates.stream().filter(m -> !carForAdminList.stream().map(d -> d.getCarNum().replace(&quot; &quot;,&quot;&quot;)).collect(Collectors.toList()).contains(m.getCarNum().replace(&quot; &quot;,&quot;&quot;))).collect(Collectors.toList());7 X  ?, z" O: ^9 D8 M  n
运行后报错:4 j6 F9 y" r% P& |" w9 O) f
java.lang.ClassCastException: java.util.HashMap cannot be cast to CarForAdmin;

+ K. j/ i$ m, u' p' K0 b+ V
6 y" d9 r. u0 Z! l# K5 Q我知道答案 回答被采纳将会获得5 金钱 已有0人回答
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则