Java two list to save the Bean object, find one of the attributes of the different elements __java

Source: Internet
Author: User

In Java, use the list collection to save objects, if you want to find two different parts of the list, you can use the ArrayList contains method, traversing each object to determine whether it is equal, as follows:

public static void Getuncontain (List<string> list1, list<string> list2) {for
        (String str1:list1) {
            if (!list2.contains (str1)) {
                //print out List2 does not have b,d
                System.out.println ("ArrayList2 is not ==>" + str1);
            }
        for (String str2:list2) {
            if (!list1.contains (str2)) {
                //print out List1 no f,g System.out.println
                (" ArrayList1 is not ==> "+ str2);}}
            }
    

But the list holds the Bean object defined in Java.
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;
    }
    }

At this point if you still use the Contains method to judge because it is an object, each time is not the same.

So how do you sift through the two different parts of the list? 1. This method is rather stupid, but it is also easiest to think of a new list to record the same place between the two lists, and then each list will remove the same part:

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))//Save the same part.
                }
            }
        }
        for (person a:l2) {
            int flag=0;
            for (person person:newlist) {
                if (Person.getname (). Equals (A.getname ())) {
                    flag=1;//the same part
                }
            }
            if (flag==0) {
                diffelist.add (a);
            }
        }
        for (person person:diffelist) {
            System.out.println ("Diffelist:" +person.tostring ());
        }
    }
2. The second method Novice is not easy to think, is to rewrite the bean inside the Equals method:
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). GetName ()) return
            true;/ /This is judged by name.
        else {return
            false;
        }
    }
}

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

    

In fact, you see the contains method source, you will find that it is the bottom of the Equals method to achieve,

The IndexOf method is called directly in the Contains method, and the Equals method is used to judge the IndexOf method

/** * 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&lt
;/tt> such that * <tt> (o==null e==null:o.equals (e)) </tt>.  * * @param o element whose presence in this list are to being 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 the ' the ' of the ' occurrence ' of the specified element * in this list, or-1 if this list does not Conta
In 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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.