Skip to content

java.util.Comparator is not suitable for distinct and distinctUntilChanged #395

Closed
@samuelgruetter

Description

@samuelgruetter

In C#, distinct and distinctUntilChanged take an IEqualityComparer (which provides Equals and GetHashCode).
That's not the same as java.util.Comparator (which provides compare).

Or mathematically speaking: There are two kinds of comparators:

  • Those which define a total ordering: java.util.Comparator and C#'s IComparer
  • Those which define an equivalence relation: C#'s IEqualityComparer, RxJava's Func2<T, T, Boolean>, and unfortunately no standard Java interface

The problem is that for distinct and distinctUntilChanged, we're not interested in an order on the elements, but only in equality.

So it would be better to use Func2<? super T, ? super T, Boolean> equality (just like sequenceEqual) instead of Comparator<U> equalityComparator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions