How to compare two array lists for similar objects which differ in at least one property in java?

前端 未结 6 1978
眼角桃花
眼角桃花 2021-01-07 01:03

I have two array list. Each has list of Objects of type User.

The User class looks like below

    public class User {

    private long id;

    pri         


        
6条回答
  •  甜味超标
    2021-01-07 01:25

    The canonical approach is as follows:

    1. Write a method countDifferences that counts the number of differences between users
    2. For each object in one list, find the minimum when compared to the other lists objects
    3. Report all objects where the minimum is not 0.

    If you put weights on the different properties you can also control that e.g. a match in the ID attribute is stronger than a match in the name.

    Update: sorry, misread your comment that the ID attribute must match.

    Replace 2) with "find object which has the same ID". Other than that, I still recommend counting the number of differences. It is more flexible, as you can define thresholds for good or bad matches etc.

提交回复
热议问题