Save bean objects in two lists in java, and find elements with different attributes

In java, the List collection is used to save objects. If you want to find different parts of two lists, you can use the contains method of ArrayList to traverse each object and determine whether they are equal, as follows:

public static void getUncontain(List<String> list1, List<String> list2){
        for(String str1 : list1){
            if(!list2.contains(str1)){
                // 打印出list2没有b,d
                System.out.println("ArrayList2里没有的是==>" + str1);
            }
        }
        for(String str2 : list2){
            if(!list1.contains(str2)){
                // 打印出list1没有f,g
                System.out.println("ArrayList1里没有的是==>" + str2);
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

But what about the bean objects defined in java that are stored in the List? 
Such as:

public class person {

    private int id;
    private String name;



    public person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

At this time, if you also use the contains method to judge that it is an object, it will be different every time.

So how to filter out the different parts in the two lists?

  • 1. This method is relatively stupid, but it is also the easiest to think of. It is to use a new list to record the same places between the two lists, and then remove the same parts from each list:
private static void getDifferentPart1(List<person> l1, List<person> l2) {
        List<person> newList=new ArrayList<>();
        List<person> diffeList=new ArrayList<>();
        for(person a : l1){
            for(person b:l2){
                if(b.getName().equals(a.getName())){
                    newList.add(a);//保存相同的部分。
                }
            }
        }
        for(person a:l2){
            int flag=0;
            for (person person : newList) {
                if(person.getName().equals(a.getName())){
                    flag=1;//相同的部分
                }
            }
            if(flag==0){
                diffeList.add(a);
            }
        }
        for (person person : diffeList) {
            System.out.println("diffeList:"+person.toString());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 2. The second method is not easy for novices to think of, that is to rewrite the equals method in the bean:
public class person {

    private int id;
    private String name;



    public person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "id:"+id+","+"name:"+name+" ";
    }
    @Override
    public boolean equals(Object obj) {
        if(name.equals(((person)obj).getName()))
            return true;//这里以name为判定标准。
        else {
            return false;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

Then you can use contains to compare objects:

private static void getDifferentPart2(List<person> l1, List<person> l2) {
        for(person a : l1){
            if(l2.contains(a)){
                System.out.println("ok!!!");
            }
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

In fact, if you look at the source code of the contains method, you will find that the bottom layer is implemented by the equals method.

The indexOf method is called directly in the contains method, and the equals method is used in the indexOf method to judge

/**
* Returns <tt>true</tt> if this list contains the specified element.
* More formally, returns <tt>true</tt> if and only if this list contains
* at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this list is to be tested
* @return <tt>true</tt> if this list contains the specified element
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}

/**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802058&siteId=291194637