Reputation: 2520
Collegues, I have two lists of different objects.
List<SharesEntity> MainSystemSecurities;
List<SecureEntity> SupportSystemSecurities;
Objects SharesEntity
and SecureEntity
have one same fiels ISIN.
In some case ISINs are the same in object of theese collections, but in some cases there are different ISINs in SupportSystemSecurities
and MainSystemSecurities
collection's objects.
I need to understand which objects (better to tell ISINs) from SupportSystemSecurities
list are absence in MainSystemSecurities
.
How to do it? What is better way to compare two collection (to compare filedls of the collection's objects)?
Upvotes: 1
Views: 361
Reputation: 81
In the most simple way you can use a loop and if statement to compare each object in both lists and then store the different objects in new lists. Your if statement will be like
If (MainSystemSecurities [i].ISIN == SupportSystemSecurities [j].ISIN){
List1.add (MainSystemSecurities [i]);
List2.add (SupportSystemSecurities [j]);
}
Upvotes: 1
Reputation: 140309
Build a Map<IsinType, SharesEntity>
from the first list:
Map<IsinType, SharesEntity> sharesEntityMap =
MainSystemSecurities.stream().collect(
Collectors.toMap(SharesEntity::getIsin,
Functions.identity()));
Then you can iterate the other list, looking up entities from the first list in this map:
for (SecureEntity secureEntity : SupportSystemSecurities) {
SharesEntity correspondingSharesEntity = sharesEntityMap.get(secureEntity.getIsin());
// ...
}
This assumes that there is a single item per ISIN in the first list. If that's not the case, you can build a Map<IsinType, List<SharesEntity>>
instead, and proceed similarly (or use a Multimap
).
Upvotes: 2