比较两个列表并找出差异
Compare two Lists and get the difference
我有两个列表 - 一个来自 xlsx 文件,第二个 - select 来自数据库
@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()));
}
}
我需要找到这些列表之间的差异,并将差异结果打印到控制台。
我已经尝试过很多路由器,但很可能不是实现,而是其他原因。
如果您使用的是 apache common-collections,请尝试使用下面的选项,否则在 Joe 的 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
普通Java实现:
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);
}
我有两个列表 - 一个来自 xlsx 文件,第二个 - select 来自数据库
@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()));
}
}
我需要找到这些列表之间的差异,并将差异结果打印到控制台。 我已经尝试过很多路由器,但很可能不是实现,而是其他原因。
如果您使用的是 apache common-collections,请尝试使用下面的选项,否则在 Joe 的 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
普通Java实现:
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);
}