Objects.equals is not type-safe:

Objects.equals(1, "your mom"); // returns false
Comparator.equal(1, "your mom"); // error

However, I agree that they are both slower than Float.equal(float, float)
and Double.equal(double, double).


пн, 7 янв. 2019 г. в 01:41, <
some-java-user-99206970363698485...@vodafonemail.de>:

> If the developer implemented the Comparable interface correctly, the
> method you proposed would be
> equivalent to java.util.Objects.equals(Object, Object).
>
> Additionally both variants would require boxing for primitive types which
> I initially wanted to prevent.
>
> > Zheka Kozlov <orionllm...@gmail.com> hat am 6. Januar 2019 um 11:56
> geschrieben:
> >
> >
> > Why don't we just add a generic method which compares any instances of
> Comparable?
> >
> >
> > public interface Comparator {
> >         ...
> >
> > public static <T extends Comparable<? super T>> boolean equal(T x, T y) {
> > return x.compareTo(y) == 0;
> > }
> > }
> >
> >
> > Usage:
> >
> > Comparator.equal(1, 1); // true
> > Comparator.equal(1.0, 1.0); // true
> > Comparator.equal(2.0f, 2.0f); // true
> > Comparator.equal("abc", "def"); // false
> > Comparator.equal("abc", 1); // compilation error
>

Reply via email to