I have two Pojo Classes on with different field with unique ID.
I want to perform intersection of two List<A>
and List<B>
.
What is best way to do. One is i can simply iterate twice and but then the complexity is too high n2.
is any better way to do that ? Can i Do it with Comparator?
Class A {
Id, Name ,DOB}
Class B{
id, aid ,location }
I have list of A , and List of B
now want to get list of A with location in B
Apache Commons Collections has a method to do this: CollectionUtils.intersection. It doesn't use generics, however.
There is also this SO question: List intersection in java
Sort both the list in increasing order of Id.
Start with List A, and find corresponding index in List B. Remeber current index of List B. say (indB)
Start with next index of List A, and compare in List B starting from (indB+1)
repeat from step 1 to 4, until either List A is ended OR List B is ended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With