-1

My purpose is to compare two list from excel and delete those those that are non duplicates. Here in my code I first add the second list to the first. Then I want to loop trough the first to identify each element , in the same loop i want to pick use the first element that i Identified to loop trough again the whole list to see if it can find any duplicate of it and if it finds it add it to a new list. And in the end i want to delete the two list and save the new one so I have a fresh with all the duplicate numbers.

 public void compareLists(){

    getOutCells1().addAll(getOutCells2());

    for(int i =0;i<getOutCells1().size();i++){

       getOutCells1().get(i);

      for( int e=0;e<getOutCells1().size();e++){

          getOutCells1().get(e);

          if(getOutCells1().get(e)==getOutCells1().get(i)){
              System.out.print(getOutCells1().get(e)+" ");
          }
      }
             }

       }

Using:

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

3 Answers3

0

I'm not sure if I understood Your question well, but I'll try to answer. First I'll assume You don't mind if duplicates were in first list or second list before connecting them - You just want to find the duplicates in the connected list. If that's the case, You may want to look here as Your implementation has many mistakes: Identify duplicates in a List

If Your aim is to find elements which are both in the first list and in the second list, the easiest approach would be to:

  1. delete duplicates from first list

  2. delete duplicates from the second list

  3. connect the two lists and find duplicates in the connected list (for this step You may check the link above)

The steps 1 & 2 You may do using java streams:

List<Integer> list = new ArrayList<>();
list = getOutCells1().stream()
                .distinct()
                .collect(Collectors.toList());
Community
  • 1
  • 1
jacobek09
  • 31
  • 3
0

I did the following and it worked.

public void compareLists(){
    getOutCells1().addAll(getOutCells2());

        for(int i=0;i<getOutCells1().size();i++){

           String a=getOutCells1().get(i).toString();

            doubleCellList.add(Double.parseDouble(a));
        }

        findDuplicates(doubleCellList);

       }

public Set<Double> findDuplicates(List<Double> doubleCellList)
{
    final Set<Double> setToReturn = new HashSet();
    final Set<Double> set1 = new HashSet();

    for (Double yourInt : doubleCellList)
    {
        if (!set1.add(yourInt))
        {
            setToReturn.add(yourInt);
        }
    }

    newDoubleCellList.addAll(setToReturn);
    return setToReturn;
}
0

You should only use retainAll() in your Set.

Set<String> intersection = new HashSet<>(getOutCells1());
intersection.retainAll(getOutCells2())
return union;

If getOutCells1() is list of Object, you should override it by Hashcode() and Equal()

Toi Nguyen
  • 413
  • 1
  • 6
  • 17