Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd move to lists and solve it this way:<br/></p> <ol> <li>Sort both lists by id ascending using custom <a href="http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/util/Comparator.html" rel="nofollow noreferrer">Comparator</a> if objects in lists aren't <a href="http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/lang/Comparable.html" rel="nofollow noreferrer">Comparable</a></li> <li>Iterate over elements in both lists like in merge phase in <a href="http://en.wikipedia.org/wiki/Merge_sort" rel="nofollow noreferrer">merge sort algorithm</a>, but instead of merging lists, you check your logic.</li> </ol> <p>The code would be more or less like this:</p> <pre><code>/* Main method */ private void execute(Collection&lt;Foo&gt; oldSet, Collection&lt;Foo&gt; newSet) { List&lt;Foo&gt; oldList = asSortedList(oldSet); List&lt;Foo&gt; newList = asSortedList(newSet); int oldIndex = 0; int newIndex = 0; // Iterate over both collections but not always in the same pace while( oldIndex &lt; oldList.size() &amp;&amp; newIndex &lt; newIndex.size()) { Foo oldObject = oldList.get(oldIndex); Foo newObject = newList.get(newIndex); // Your logic here if(oldObject.getId() &lt; newObject.getId()) { doRemove(oldObject); oldIndex++; } else if( oldObject.getId() &gt; newObject.getId() ) { doAdd(newObject); newIndex++; } else if( oldObject.getId() == newObject.getId() &amp;&amp; isModified(oldObject, newObject) ) { doUpdate(oldObject, newObject); oldIndex++; newIndex++; } else { ... } }// while // Check if there are any objects left in *oldList* or *newList* for(; oldIndex &lt; oldList.size(); oldIndex++ ) { doRemove( oldList.get(oldIndex) ); }// for( oldIndex ) for(; newIndex &lt; newList.size(); newIndex++ ) { doAdd( newList.get(newIndex) ); }// for( newIndex ) }// execute( oldSet, newSet ) /** Create sorted list from collection If you actually perform any actions on input collections than you should always return new instance of list to keep algorithm simple. */ private List&lt;Foo&gt; asSortedList(Collection&lt;Foo&gt; data) { List&lt;Foo&gt; resultList; if(data instanceof List) { resultList = (List&lt;Foo&gt;)data; } else { resultList = new ArrayList&lt;Foo&gt;(data); } Collections.sort(resultList) return resultList; } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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