0

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,

Koitoer
  • 18,778
  • 7
  • 63
  • 86
Prashant Bhagwani
  • 25
  • 1
  • 3
  • 10
  • Loop through both lists and do a comparison...? – But I'm Not A Wrapper Class Feb 06 '14 at 19:14
  • Hi Mohammad there is huge amount of data in both lists around 5 lac in one list it may reduce the performance – Prashant Bhagwani Feb 06 '14 at 19:18
  • possible duplicate of [Intersection and union of ArrayLists in Java](http://stackoverflow.com/questions/5283047/intersection-and-union-of-arraylists-in-java) – Elliott Frisch Feb 06 '14 at 19:20
  • 500,000 (I'm assuming that's what 5 lac is, according to Wikipedia) entries in one list? I'm curious: why not try and do this in the database? – Chris Forrence Feb 06 '14 at 19:20
  • 1
    I don't really think List is the best way to store your data. That, of course, depends on later usage. And I don't phantom how you store 2 values as a list element. I would suggest using Map, it consist of a unique identifier (investment number) and a value, and has a method to check if an identifier is in the map. – WrongASP Feb 06 '14 at 19:23
  • Hi Chris, I have already filtered out as much as possible both lists are coming from saperate databases – Prashant Bhagwani Feb 06 '14 at 19:24
  • 1
    @WrongASP You use an object type (I hope); also, I assume you mean fathom (or it's very [ghostly](http://en.wikipedia.org/wiki/Danny_Phantom)). – Elliott Frisch Feb 06 '14 at 19:26
  • @PrashantBhagwani: Ok, so they're in different databases (not just different database tables). And just to clarify what you're trying to do, you've got your List1 and List2. You're scanning through List1 and checking to see if there is a matching Investment Number in List2. If there's not, then you're removing that element from List1 (or if there is a match, you're adding that element to List3, which contains the matches). Correct? – Chris Forrence Feb 06 '14 at 19:27
  • @Elliott Frisch: I meant fathom. Sorry. – WrongASP Feb 06 '14 at 19:38

3 Answers3

4

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]
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
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).

DwB
  • 37,124
  • 11
  • 56
  • 82
0

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

Girish
  • 1,717
  • 1
  • 18
  • 30