回答

收藏

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

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

一、需求逻辑:6 |) O8 W6 f1 j% ~7 ~3 W
1.先取出所有的车辆信息listWithoutDuplicates,并去重(该List数据最全,元素均为CarVo);6 `- G9 Z" j4 D. Z0 a" z
2.再从另一个表中取出车辆信息carForAdminList;(数据较少,元素均为CarForAdmin);6 U: y( {  v  p
3.根据carForAdminList中的CarForAdmin车牌号是否存在于listWithoutDuplicates中为条件(即CarForAdmin.车牌号等于CarVo.车牌号),得出listWithoutDuplicates中多出的元素,并形成一个newTransferList  T- b5 Y% y% H0 N& D
二、问题:8 ^( Y0 u: p$ `
使用以下代码进行取差集:
    % k" V1 T$ D& ~; l  H" O
  • List<CarVo> listWithoutDuplicates = CarDao.pageVoList(param);
    ' w5 U7 G& d+ I; z% j
  • List<CarForAdmin> carForAdminList = CarDao.pageList(param);
    , B$ f1 v* L& K  u5 ^, O
  • 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());2 ?, c: X( u* E' B6 |
运行后报错:! t$ ?$ j; s( Z
java.lang.ClassCastException: java.util.HashMap cannot be cast to CarForAdmin;
  F, `6 ?2 {) E+ b
( O# M6 c1 f' i. B! l* c0 H* t% c2 L
我知道答案 回答被采纳将会获得5 金钱 已有0人回答
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则