Let's say my first list is:
[{tenancyNumber:777,no:1}, {tenancyNumber:888,no:2}, {tenancyNumber:999,no:3}]
And my second List is
[{tenancyNumber:444,no:4}, {tenancyNumber:999,no:5}, {tenancyNumber:666,no:6}]
So, if we compare by name in the two lists there are 2 objects in the second list which are not in the first list. How to find out this in Java?
This is what i tried:
final Long newAccounts = endDateData.stream()
.map(TenancyHistory::getTenancyNumber)
.filter(startDateData.stream().map(TenancyHistory::getTenancyNumber)::equals)
.count();
which is throwing an error
[PredicateIncompatibleType] Predicate will always evaluate to false because types Stream and Long are incompatible
You can first create a lookup Set
of tenancy numbers for easing in the filter while you iterate on the second list.
Set<Long> tenancies = startDateData.stream()
.map(TenancyHistory::getTenancyNumber)
.collect(Collectors.toSet());
Now the attempt you made can be fixed as;
final Long newAccounts = endDateData.stream()
.map(TenancyHistory::getTenancyNumber)
.filter(num -> !tenancies.contains(num)) // here
.count();
To help you understand the error message that you were facing, consider the code
(end/start)DateData.stream().map(TenancyHistory::getTenancyNumber)
this would return a Stream<Long>
, now what you ended up performing in your filter
stage was to compare Stream<Long>
to a Long
value of tenancy number from another Stream
as
startDateData.stream().map(TenancyHistory::getTenancyNumber)::equals
and hence the error message read this would evaluate always to false
. Such that resulting value of your would always be 0
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.