I am trying to get the intersection between Two List in JAVA, Please find the List below
LIST 1:
List<EligViewEventSubject> list1= new ArrayList<>();
list1= [EligViewEventSubject [event=EOD, subjectTypeCd=HOLDINGS, region=IA], EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA]]
LIST 2:
List<EligViewEventSubject> list2= new ArrayList<>();
list2 = [EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA]]
The Result for the above should be Like below
List<EligViewEventSubject> resultList = new ArrayList<>();
resultList = [EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA]]
In short i want the Matching ones alone(intersection)
I have tried the below:
List<EligViewEventSubject> unionList = new ArrayList<>(list1);
unionList.addAll(list2);
List<EligViewEventSubject> intersectionList = new ArrayList<>(list1);
intersectionList.retainAll(list2);
Union List I get as:
[EligViewEventSubject [event=EOD, subjectTypeCd=HOLDINGS, region=IA], EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA], EligViewEventSubject [event=EOD, subjectTypeCd=ASSETS, region=IA]]
Intersection is []
Please share your thoughts on this.