Issue
I want to get intersection and difference between List<HashMap<String, Object>> but below code just show true or false.
I want to get difference like [{prodCode=KR7279570006, accountNum=20101834049, orgCode=C1AACQ0000}]
I want to get intersection like [{prodCode=KR7020150009, accountNum=20402786856, orgCode=C1AACQ0000}, {prodCode=KR7020150009, accountNum=20101834049, orgCode=C1AACQ0000}]
List<HashMap<String, Object>> stockListInDb =
[{prodCode=KR7020150009, accountNum=20402786856, orgCode=C1AACQ0000},
{prodCode=KR7020150009, accountNum=20101834049, orgCode=C1AACQ0000}]
List<HashMap<String, Object>> presentedStockList =
[{prodCode=KR7020150009, accountNum=20402786856, orgCode=C1AACQ0000},
{prodCode=KR7020150009, accountNum=20101834049, orgCode=C1AACQ0000},
{prodCode=KR7279570006, accountNum=20101834049, orgCode=C1AACQ0000}]
System.out.println("새로운 주식은 " + presentedStockList.removeAll(stockListInDb) );
System.out.println("홀딩한 주식은 " + stockListInDb.retainAll(presentedStockList));
새로운 주식은 true
홀딩한 주식은 true
Solution
I have also tried retainAll, but it's not working instead of using this use: If you want the intersection of List1 and List 2 it will remove all elements from the list1 that are present in list 2. if you want the differencece just replace the List1 to list2 and vice versa.
List1.stream().filter(List2::contains).collect(Collectors.toList());
Answered By - Sandeep Kumar Nat
Answer Checked By - Katrina (JavaFixing Volunteer)