We help IT Professionals succeed at work.

how to compare two different <ArrayList>>String> objects for duplicates

areyouready344
on
For example, i have two different ArrayList objects

ArrayList<String> column1 = new ArrayList<String>();
ArrayList<String> column2 = new ArrayList<String>();

column1 object contains 200 text lines
column2 object contains 400 text lines

I would like to find out if each column1 text lines are duplicate in column2 text lines and save all un-duplicate text lines into a file.

Here's what I'm done so far:

                   
                      for(String textLineA: columnA = h.get(getAllComboSelections[0]))
                      {
                                  Pattern columnA = Pattern.compile(textLineA);          
                                  Matcher matchM = columnA.matcher(textLineA);
                                  if(matchM.find())
                                  {
                                         for(String textLineB: columnB = h.get(getAllComboSelections[0]))  
                                          Matcher matchM = columnB.matcher(textLineB);    
                                         System.out.println(columnB);
                                  }
                       }

Comment
Watch Question

Shahan AyyubSenior Software Engineer

Commented:
You can do like this: (pseudocode)

in between for loop from i to n
   if(column1.contains( column2(i) ))   where i representing ith element
         // duplicate
   else
        // do stuff

Author

Commented:
Shahan, Are you saying use a numerical for loop instead of a collection-based for loop?
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
If your ultimate goal is to get unique lines, you can simply put the values of both ArrayList objects into a Set.
Only unique values will be in the Set, so you can then just write out the Set values to file.
Shahan AyyubSenior Software Engineer

Commented:
No it does not matter which loop you use, the idea behind my post is to use CONTAIN() method.
Shahan AyyubSenior Software Engineer

Commented:
See this sample code:

ArrayList<String>  column1 = new ArrayList<String>();
       ArrayList<String> column2 = new ArrayList<String>();

       //Adding item not for you, you already have 400 and      // 200 items        
 
       //column1.add("item1");  
       //column1.add("item2");
       //column1.add("item3");
       //column1.add("item4");

       //column2.add("item5");
       //column2.add("item6");
       //column2.add("item7");
       //column2.add("item4");

        // assuming column1.size() has smaller or equal
        // size as compared to column2
        for (int i = 0; i < column1.size() ; i++)
        {
       if(column1.contains(column2.get(i)))
           System.out.println("Element exists in 2");
       else
           System.out.println("Element Not exists in 2");

Open in new window

Chief Technology Officer
Most Valuable Expert 2011
Commented:
But really, I would simply do this:

ArrayList<String> column1 = new ArrayList<String>();
ArrayList<String> column2 = new ArrayList<String>();
// instantiate a set using column1
Set<String> uniqueColumns = new TreeSet<String>(column1);
// add non-existent elements from column2
uniqueColumns.addAll(column2);

Open in new window