irobertson / pojomatic

Pojomatic provides configurable implementations of the equals(Object), hashCode() and toString() methods inherited from java.lang.Object.
30 stars 7 forks source link

Order insensitive array/list comparison #9

Closed florindaneliuc-cs closed 8 years ago

florindaneliuc-cs commented 8 years ago

Is there a way to accomplish this with Pojomatic?

irobertson commented 8 years ago

The only way to do this is to expose the list as a set. For example:

public class OrderlessListHolder<T> {
  @Property(policy=TO_STRING)
  private List<T> list;
  public List<T> getList() { return list; }
  public void setList(List list) { this.list = list; }

  @Property(policy=HASHCODE_EQUALS)
  private Set<T> getListAsSet() {
    return new HashSet<>(list);
  }
}

In this way, when pojomatic does equals hashcode computation, it will be using a Set representation of the list, rather than the original list representation. Note that if equals or hashCode is likely to be called often, it may make sense to cache the set representation instead of recreating it each time.

While I assume that it is not an option in your case, I would be remiss if I failed to point out that the easiest solution might be to just change your list to a set,