質問

 public int compareTo(DynamicField other) {
     return other.wildcard.length() - wildcard.length();
 }

 boolean equals(DynamicField other) { 
    return compareTo(other) == 0; 
 } 

I'm facing this issue. Can you Please suggest me how to solve this ? My method implementations are as above

役に立ちましたか?

解決

Your equals method is not correctly defined, it should be:

@Override
public boolean equals(Object obj)
{
  ...
}

Something like this should work:

@Override
public boolean equals(Object obj)
{
  if (!(obj instanceof DynamicField))
     return false;
  return compareTo((DynamicField)obj) == 0;
}

If you're overriding a method of another class or implementing an interface method, you should always add @Override. If you defined it incorrectly, it should tell you something like:

method does not override or implement a method from a supertype
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top