How to compare elements of two generic lists in java?
<p>I have two generic list of Data Type class:RequiredList &amp; PrimaryList</p>

<pre><code>   public class Data{
        private String dataCode;
        private String dataDesc; 

        public String getDataCode(){}

        public void setDataCode(String dataCode){}

        public String getDataDesc(){}

        public void setDataDesc(String dataDesc){}


        }

    public static void main(String[] args) {
        List&lt;Data&gt; primaryList = new ArrayList&lt;Data&gt;();
        for (int i = 2; i &lt; 5; i++) {
            Data data = new Data();
            data.setDataCode("code" + i);
            data.setDataDesc("desc" + i);
            primaryList.add(data);
        }

        List&lt;Data&gt; requiredList = new ArrayList&lt;Data&gt;();
        for (int i = 0; i &lt; 8; i++) {
            Data data = new Data();
            data.setDataCode("code" + i);
            data.setDataDesc("desc" + i);
            requiredList.add(data);

        }
        StringBuffer partyList = new StringBuffer();
        for (int i = 0; i &lt; requiredList.size(); i++) {
            if (primaryList.size() &gt; i
                    &amp;&amp; !requiredList.get(i).getDataCode().equals(primaryList.get(i).getDataCode())) {
                partyList.append(requiredList.get(i).getDataDesc());
                partyList.append(",");

            }
            if(primaryList.size() &lt;= i)
            {
                partyList.append(requiredList.get(i).getDataDesc());
                partyList.append(",");
            }           
        }
        System.out.println(partyList.toString());
    }
</code></pre>

<p>I want to compare elements of requiredList to elements of primaryList.Those elements of primaryList are not present in requiredList , they will be added in new list.Can anybody help me out?</p>
