title: From RxJava2, How can I compare and filter two observables if the values are equal?
body: <p>I am new to RxJava2.</p>
  
  <p>I am trying to get a list of Transaction object both from cache and from server.
  I want to compare the server value to cache value and if the server value is the same, then ignore it.</p>
  
  <p>I was able to do it easily using .scan() because we can return null and when null is returned from the .scan() the value got ignored(filtered).</p>
  
  <p>RxJava 1</p>
  
  <pre><code>private Observable&lt;List&lt;Transaction&gt;&gt; getTransactionsFromCacheAndServer() {
      return Observable.concat(
              getTransactionsFromCache(),
              getTransactionsFromServer()
      )
              .scan((p1, p2) -&gt; {
                  if (p1 == null &amp;&amp; p2 != null) {
                      return p2;
                  } else if (p1 != null &amp;&amp; !isListSame(p1, p2)) {
                      return p2;
                  } else {
                      return null;
                  }
              });
  }
  </code></pre>
  
  <p>With RxJava 2, since I cannot return null anymore, things are not easy.</p>
  
  <p>RxJava 2</p>
  
  <pre><code>    private Observable&lt;List&lt;Transaction&gt;&gt; getTransactionsFromCacheAndServer() {
      return Observable.concat(
              getTransactionsFromCache(),
              getTransactionsFromServer()
      )
              .map(FilterObject::new)
              .scan((filterObject1, filterObject2) -&gt; {
                  List&lt;Transaction&gt; p1 = (List&lt;Transaction&gt;)filterObject1.value;
                  List&lt;Transaction&gt; p2 = (List&lt;Transaction&gt;)filterObject2.value;
  
                  if (p1.size() == 0 &amp;&amp; p2.size() &gt; 0) {
                      return filterObject2;
                  } else if (!isListSame(p1, p2)) {
                      return filterObject2;
                  } else {
                      filterObject2.filter = true;
                      return filterObject2;
                  }
              })
              .filter(filterObject -&gt; !filterObject.filter)
              .map(filterObject -&gt; (List&lt;Transaction&gt;)filterObject.value);
  }
  </code></pre>
  
  <p>Where FilterObject is:</p>
  
  <pre><code>public class FilterObject {
  public Object value;
  public boolean filter;
  
  public FilterObject(Object value) {
      this.value = value;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Even though I can achieve the same thing using above method, it seems very ugly. Also I had to include two maps which might not be so performance friendly.</p>
  
  <p>Is there a simple/clean way to achieve what I want?</p>