Is there a Map in Java or 3rd-party libraries preserving insertion order (similar to LinkedHashMap) which would also check order of keys during comparison (equals()
and hashCode()
methods) ? So that Map(a => 1, b => 2)
would be different from Map(b => 2, a => 1)
.
Similar to below but I don't want to reinvent and maintain the wheel:
public class EqualityStrictLinkedMap<K, V> extends ForwardingMap<K, V>
{
...
@Override
public boolean equals(Object o)
{
if (!(o instanceof Map))
return false;
Map<K, V> that = (Map<K, V>) o;
return Iterables.elementsEqual(entrySet(), that.entrySet());
}
@Override
public int hashCode()
{
int h = 1;
for (Map.Entry<K, V> entry: entrySet()) {
h = (h*31 + Objects.hashCode(entry.getKey()))*31 + Objects.hashCode(entry.getValue());
}
return h;
}
}