问题
I am getting data from the two different databases and storing them in the java.util.List
Elements of database:1
column 1 column 2
**Investment Number** Investment Name
123 abcwe
124 agsdf
454 lkjcv
784 ojncv
478 hfdgh
852 qweyu
745 mmkty
201 pbckl
560 jklfg
741 nbvbn
storing them in list1
Elements of database:2
column 1 column 2
Investment Number Property Number
123 548
980 743
454 200
350 357
478 698
852 223
745 795
784 213
341 022
741 900
storing them in list2
need to compare according to the Investment Numbers if Investment Number of list1 available in list2 then need to keep and discard others.
Thanks,
回答1:
If you have two list, you can do the following:
List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 5, 6, 7));
List<Integer> list2 = new ArrayList<>(Arrays.asList(5, 6, 7, 8, 9));
list1.retainAll(list2);
System.out.println("list1 = " + list1);
And the result will be:
list1 = [5, 6, 7]
回答2:
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).
回答3:
Suppose you have two pojos corresponding to two different table of two different datbase
Pojo1-------entity class corressponding to first table of first database
Pojo2-------entity class corressponding to second table of second database
List<Pojo1> listOne = new ArrayList<Pojo1>();
List<Pojo2> listTwo = new ArrayList<Pojo2>();
listTwo.retainAll(listOne);
Note that in these two pojos you have to override hashcode() and equals() method of Object class, because in retainAll, you are comparing the objects of two pojos
来源:https://stackoverflow.com/questions/21611881/compare-2-lists-in-java