【发布时间】:2020-05-31 17:11:50
【问题描述】:
我正在尝试在 JAVA 中获取两个列表之间的交集,请在下面找到列表
列表 1:
List<EligViewEventSubject> list1= new ArrayList<>();
list1= [EligViewEventSubject [event=EOD, subjectTypeCd=HOLDINGS, region=IA], EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA]]
清单 2:
List<EligViewEventSubject> list2= new ArrayList<>();
list2 = [EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA]]
上面的结果应该如下所示
List<EligViewEventSubject> resultList = new ArrayList<>();
resultList = [EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA]]
简而言之,我只想要匹配的(交叉点)
我已经尝试过以下方法:
List<EligViewEventSubject> unionList = new ArrayList<>(list1);
unionList.addAll(list2);
List<EligViewEventSubject> intersectionList = new ArrayList<>(list1);
intersectionList.retainAll(list2);
我得到的联合列表:
[EligViewEventSubject [event=EOD, subjectTypeCd=HOLDINGS, region=IA], EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA], EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA]]
交点是[]
请分享您对此的看法。
【问题讨论】:
标签: java list arraylist collections stream