java兩個list中儲存bean對象,找出其中某一屬性不同的元素__java

來源:互聯網
上載者:User

在java中運用List集合儲存對象,如果想找到兩個list中不同的部分,可以用ArrayList的contains方法,遍曆每一個對象,判斷是否是相等的,如下:

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);            }        }    }

但是List中儲存的是java中定義的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;    }    }

此時如果還用contains方法判斷由於是對象,每一次都不相同。

那該怎麼篩選出兩個list中不同的部分呢。 1.這個方法比較笨,但是也是最容易想到的,就是用一個新的list,來記錄兩個list之間相同的地方,然後每個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());        }    }
2.第二種方法新手不容易想到,就是重寫bean裡面的equals方法:
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;        }    }}

然後此時就可以用contains去比較對象:

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

其實你看contains方法源碼的話,會發現它底層就是用equals方法實現的,

contains方法中直接調用indexOf方法,indexOf方法中採用equals方法判斷

/*** 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.