Advanced filtering with distinct calls

The current item existence checking algorithm (.contains()) isn't optimal because it can require O(n) time to execute, and it can be slow if the data history is long.

The core problem that we are facing here is that financial stock quotes do not change and, when they are not changed, we would rather avoid adding unnecessary entries into the RecyclerView. It means that we want to add a new value to the RecyclerView only when it's different from the previous one.

We can probably use the .distinct() call on the Observable that emits financial stock quotes. However, it won't work in our case because it ensures uniqueness of values for the entire lifetime of the Observable. In our case, the values can happen to be the same; for example, on the 10th and 100th emissions, we should still record these.

.distinct() uses sets internally to do the checks for the existence of the items; so the checks are relatively fast (O(log(n)) time), but it still requires memory to hold all the objects ever received.
Also, it means that the classes that will be using are in such a way need to have the .equals() and .hashCode() methods properly implemented.

Another option is to use .distinctUntilChanged(), which only lets items through if they differ from the last value. This would be fine but, in our case, that will almost always be true--the items will always have at least the symbol (stock) name different.

We need a way to group items by their stock name and then use .distinctUntilChanged().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87