Create a wrapper class for your two classes and use List.retainAll().
Here is some code
You already have classes that are the equivalents of Blam and Hoot;
they are included for completeness
public class Blam
{
private String investmentNumber;
private String investmentName;
... getters and setters
}
public class Hoot
{
private String investmentNumber;
private String propertyNumber;
... getters and setters
}
public class Wrapper
{
private Blam blam;
private Hoot hoot;
public Wrapper(final Blam blam)
{
this.blam = blam;
}
public Wrapper(final Hoot hoot)
{
this.hoot = hoot;
}
public boolean equals(Object oThat)
{
if (this == oThat)
{
return true;
}
if (oThat instanceof Wrapper)
{
Wrapper wThat = (Wrapper)oThat;
String myInvestmentNumber;
String yourInvestmentNumber;
if (blam != null)
{
myInvestmentNumber = blam.getInvestmentNumber();
}
else
{
myInvestmentNumber = hoot.getInvestmentNumber();
}
if (wThat.blam != null)
{
yourInvestmentNumber = wThat.blam.getInvestmentNumber();
}
else
{
yourInvestmentNumber = wThat.hoot.getInvestmentNumber();
}
return StringUtils.equals(myInvestmentNumber, yourInvestmentNumber);
}
return false;
}
public int hashCode()
{
... implement this using blam and hoot as in equals() above.
}
... getters for Hoot and Blam values as needed.
}
List<Wrapper> listOne = new ArrayList<Wrapper>();
List<Wrapper> listTwo = new ArrayList<Wrapper>();
... populate listOne with wrapped Blam values.
... populate listTwo with wrapped Hoot values.
listTwo.retainAll(listOne);
After executing the retainAll, the Hoot list contains all the values from database 2 for which there was a corresponding entry in database 1.
Note: StringUtils is an apache commons lang class that performs a null safe equals (and other stuff).