Feature suggestion: Add static equals methods to Float and Double

some-java-user-99206970363698485155 at vodafonemail.de some-java-user-99206970363698485155 at vodafonemail.de
Sun Jan 6 18:41:02 UTC 2019


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 <orionllmain at 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


More information about the core-libs-dev mailing list