Java: How to get around the lack of an equatable interface?

As far as I know, things like SortedMapor SortedSet, use compareTo(and not equals) for types Comparable<?>to check for equality ( contains, containsKey).

But what if certain types are equivalent in concept and not comparable?
(Hash codes, memory addresses, ...)

I need to declare Comparator<?>and override the method int compareTo(T o1, To2). Well, I can return 0 for instances that are considered equal. But, for unsubstantiated cases, what do I return when the order is not obvious?

Is the approach of using SortedMap or SortedSet on equatable , but (on the concept) is not comparable to the type of good, in any case?

Thank!

EDIT:
I don't want to keep things sorted, but I would use a “regular” map and set, I could not “override” the equality behavior.

EDIT 2:
Why can't I just override equals(...):
I need to change the behavior of foreign class equality. I can’t edit it.

EDIT 3:
Think of .NET: they have an IEquatable interface that the cat modifies equality behavior without touching comparable behavior.

4:
compareTo return 0 1 ? ? , , SortedMap/SortedSet compareTo . , , ? . * . , . . 1 .

5:
? ? : Comparator?

6:
Mark Peters waxwing . , equals hashCode, .

+3
13

.

public class Foreign {
  // undesired equals() and hashCode() implementation
}


public class ForeignWrapper {
   private Foreign foreign;

   public ForeignWrapper(Foreign foreign) {
      this.foreign = foreign;
   }

   public void equals() {
       // your equals implementation, using fields from foreign
   }

   public int hashCode() {
       // your hashCode implementation, using fields from foreign
   }

}

new ForeignWrapper(foreign) HashSet/HashMap. , , , .

+10

, SortedMap SortedSet , . , SortedSet. , , , , "".

HashMap/Set.

Edit # 2

, - . , .

Edit # 3

Java equals . .

Edit # 4

, 1 !

SortedSets , . . , , , A.compareTo(B) > 0, B.compareTo(A) < 0. , .

public static void main(String[] args) throws Exception {
    SortedSet<MyClass> set = new TreeSet<MyClass>();
    MyClass one = new MyClass(1);
    set.add(one);
    set.add(new MyClass(2));
    set.add(new MyClass(3));
    System.out.println(set.contains(one));
}
private static class MyClass implements Comparable<MyClass> {
    private final int data;
    private MyClass(int data) { this.data = data; }
    public int compareTo(MyClass o) { return (data == o.data ? 0 : 1); }
}

false, , , .

+10

, / .

, , HashMap HashSet ? SortedMap SortedSet, .

+6

, "" , "" .

, ?

, () O (log n) . , , - (HashMap, HashSet), O (1).

+1

SortedMap SortedSet equatable, ( ) ?

. , , ?

equals() hashcode() Map/Set.

+1

hashCode, , , HashMap .

+1

, , , , somethings, Set/Map, , Object.equals.

AbstractSet AbstractMap AbstractCollection.contains, .

, , , .

. http://java.sun.com/javase/6/docs/api/java/util/AbstractSet.html http://java.sun.com/javase/6/docs/api/java/util/AbstractCollection.html#contains(java.lang.Object)

+1

HashMap HashSet, Equality

interface Equality<T>//Defines the equality behavior
{
   int hashCode(T t);//Required, always make sure equals = true => same hashCode
   boolean areEqual(T t,Object t2);
}
class EqualWrapper<T>//Wraps object and equality for the HashMap/Set
{
   T object;
   Equality<T> equal;
   int hashCode(){return equal.hashCode(object);}
   boolean equals(Object o){return equal.areEqual(object,o);}

}
class MySet<T>extends AbstractSet<T>
{
   private HashSet<EqualWrapper<T> > internalSet = new HashSet<T>();
   private Equality<T> equal;
   public MySet(Equality<T> et){equal = et;}
   // TODO implement abstract functions to wrapp 
   // objects and forward them to 
   // internalSet  
}

, . , jre

+1

, ; , , ? , , SortedMap SortedSet. ( ? Map Set).

equals() Java Object, Object, equals(). , equals() , , .

(, HashMap HashSet), hashCode(), , hashCode() equals() (. Object , ).

0

, , SortedXXX . , , - , , , " " - , .

0

(custom) Comparator, Sorted [Set | Map] ...

javadocs : All keys inserted into a sorted map must implement the Comparable interface (or be accepted by the specified comparator).

SortedSet<MyObject> s = new TreeSet<MyObject>(new Comparator<MyObject>() {
    @Override
    public int compare(T o1, T o2) {
        // your very specific, fancy dancy code here
    }
});

http://java.sun.com/javase/6/docs/api/

0

, (, ), - Set Map, , LinkedHashSet LinkedHashMap .


: :

. .

/? TreeSet TreeMap Comparator. .

SortedSet<String> set = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);

( String ).

. :

0

, "" , "" -.

equals() . , IEquatable. SortedSet/SortedMap, , @ptomli, .

Using a HashMap / HashSet instead seems like a good suggestion. Have you looked at them?

0
source

Source: https://habr.com/ru/post/1750664/


All Articles