关于使用Lambda 表达式,根据List中的对象的某个属性,求两个List的差集,
技术问答
372 人阅读
|
0 人回复
|
2023-09-11
|
一、需求逻辑:
2 G5 R" ^) A' ?+ p1.先取出所有的车辆信息listWithoutDuplicates,并去重(该List数据最全,元素均为CarVo);: b; p+ U1 |1 J9 t( O- F# o5 e
2.再从另一个表中取出车辆信息carForAdminList;(数据较少,元素均为CarForAdmin);7 q, u' i0 h7 J. a- H; I/ f9 B
3.根据carForAdminList中的CarForAdmin车牌号是否存在于listWithoutDuplicates中为条件(即CarForAdmin.车牌号等于CarVo.车牌号),得出listWithoutDuplicates中多出的元素,并形成一个newTransferList;
6 Z5 B3 I- A! P* g3 k二、问题:
6 U# s! P; ]7 t3 h: r使用以下代码进行取差集:
( }" n% n4 r4 K6 x* u( Z6 ~. z" I: l* a8 ]0 D6 L
- List<CarVo> listWithoutDuplicates = CarDao.pageVoList(param);* h" q+ b/ G' ?5 u$ D
- List<CarForAdmin> carForAdminList = CarDao.pageList(param);
6 J+ j2 o+ B9 F" g" X2 X - List<CarVo> newTransferList = listWithoutDuplicates.stream().filter(m -> !carForAdminList.stream().map(d -> d.getCarNum().replace(" ","")).collect(Collectors.toList()).contains(m.getCarNum().replace(" ",""))).collect(Collectors.toList());- C% Z4 ~, W2 d C
- z" S4 C- k, W$ U# h2 [* M0 d' P- 运行后报错:java.lang.ClassCastException: java.util.HashMap cannot be cast to CarForAdmin;
9 `: D! h$ L; d, g+ X
8 ~" a- O0 t1 G
4 M2 k6 t8 s( F9 K! J9 c我知道答案 回答被采纳将会获得6 金钱 已有0人回答 |
|
|
|
|
|