Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you <strong>do not</strong> want to <strong>lose</strong> the <code>originalList</code> then probably this code can help</p> <pre><code>List&lt;String&gt; orgList = new ArrayList&lt;String&gt;(); orgList.add("a"); orgList.add("b"); List&lt;String&gt; newList = new ArrayList&lt;String&gt;(); newList.add("a"); newList.add("d"); newList.add("e"); newList.add("f"); List&lt;String&gt; result = new ArrayList&lt;String&gt;(); List&lt;String&gt; existingInBoth = new ArrayList&lt;String&gt;(); //Add item to result, if its present in newList BUT not in orgList for(String s : newList) { if(!orgList.contains(s)) result.add(s); } //Add the common items (this is inverse of removing the item which is there in orgList and not in newList) for(String s : orgList) { if(newList.contains(s)) existingInBoth.add(s); } result.addAll(existingInBoth); System.out.println(result); </code></pre> <p>For this to work in your case, you need to make <code>MyClass</code> override <code>equals()</code> method of <code>java.lang.Object</code> class and define the <strong>equality</strong> of the objects. Reason being, the <code>contains(Object)</code> method essentially checks the <strong>existance</strong> of an object within the collection, by checking the <strong>equality</strong>(<code>ob1.equals(obj2)</code>) of the object, with all the elements in the collection.</p> <p>The logic within the <code>equals()</code> method should be similar to the one which is written in <code>hasMyClass</code> method's <code>if</code> condition</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload