Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to Implementation of intersection, difference and union with List<HashMap<String, Object>> in java

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}]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading