1

I have two Lists which I get - one from xlsx file, the second one - by select from database

@Component
public class ReadExcelDemo implements CommandLineRunner {

    @Autowired
    private TakeDataService dataService;

    private List<Integer> list;
    private List<TakeData> dates;



    @Override
    public void run(String... args) {
        try
        {
            FileInputStream file = new FileInputStream("Report.xlsx");

            XSSFWorkbook workbook = new XSSFWorkbook(file);

            XSSFSheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        if (cell.getColumnIndex() == 0) {
                            list = new ArrayList<>();
                            list.add((int) cell.getNumericCellValue());
                            //System.out.println(list);
                        }
                    }
                }
            }
            file.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        takeData();
        printDifference();
    }

    public void takeData() {
        dates = new ArrayList<>(dataService.takeData());
       // System.out.println(dates);
    }

    public void printDifference() {

        List<Integer> id = dates.stream().map(TakeData::getId).collect(Collectors.toList());

//        for (Integer a : list) {
//            id.remove(a);
//        }
        
//List<Integer> difference = new ArrayList<>(CollectionUtils.disjunction(list, id));
        
        Set<Integer> diff = new HashSet<>(id);
        System.out.println(list.stream().filter(((Predicate<? super Integer>) diff :: contains).negate()).collect(Collectors.toList()));

    }
}

I need to find the difference between these Lists and just print the result of this difference to the console. I have already tried many routers, but most likely it's not the implementation, but something else.

PJ Fanning
  • 953
  • 5
  • 13
Alex
  • 41
  • 9
  • @Joe Yes, I saw this answer. Tried. But the output is not correct. In theory, there should be a difference. And the answer is all the data – Alex Apr 28 '22 at 12:17
  • When collecting the values from the excel sheet, there is an abundant `list = new ArrayList<>()` in the loop. With it, `list` will never contain more than one element and consecutively, `list.stream().filter(...)` will never yield more than one result. – Izruo Apr 28 '22 at 12:37

2 Answers2

1

If you're using apache common-collections, try using the below, otherwise there's plenty other options shared in Joe's link

List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(1, 4, 5);

Collection<Integer> list1WithoutList2 = CollectionUtils.removeAll(list1, list2);
System.out.println(list1WithoutList2); //Prints 2,3

Collection<Integer> list2WithoutList1 = CollectionUtils.removeAll(list2, list1);
System.out.println(list2WithoutList1); //Prints 4,5


System.out.println(Stream.concat(list1WithoutList2, list2WithoutList1).toSet()); //Prints 2,3,4,5
Jbk
  • 26
  • 2
  • If I do as you say, I get an error: Required type: Collection Provided: Collection and a: Stream extends T> Collection b: Stream extends T> Collection – Alex Apr 28 '22 at 13:07
  • If I use this part of my code: List id = dates.stream().map(TakeData::getId).collect(Collectors.toList()); with yours, then I get the entire list from the collection (not sorted) – Alex Apr 28 '22 at 13:16
  • The ordering of the list doesn't matter. If you change list1 to: List list1 = Arrays.asList(2,3,1); The result is the same – Jbk Apr 28 '22 at 13:50
1

Plain Java implementation:

public static void main(String[] args) {
    List<Integer> list = List.of(1,2,3,4,5);
    List<Integer> target = List.of(1,2,3,40,50);
    List<Integer> result = list.stream().filter(v -> !target.contains(v)).collect(Collectors.toList());

    // Prints [4, 5] - items in the list not found in the target
    System.out.println(result);
}
Delta George
  • 2,560
  • 2
  • 17
  • 11