vavr-io / vavr

vʌvr (formerly called Javaslang) is a non-commercial, non-profit object-functional library that runs with Java 8+. It aims to reduce the lines of code and increase code quality.
https://vavr.io
Other
5.68k stars 631 forks source link

discussion: should hashcode be calculated based on order #2742

Closed jarlah closed 1 month ago

jarlah commented 1 year ago

What concerns me is that changing hashCode in such a big way can affect other parts of vavr, like equals. However, it seemed to not make any difference to comparing the same two (different) hash maps I made a test for with assertThat(...).isNotEqualTo(...), which is also the example from #2733

In addition, this Collections.hasOrdered and Collections.hashUnordered is used many other places. Should we change them too?

Look at this as a discussion PR, more than actual fix.

DONTMERGE :) (unless it was an ingenious fix, which I doubt)

PS! is this project used actively still? to be honest, I would rather use vavr than mock around with streams and collect and wierd reduce mechanics.

pivovarit commented 1 month ago

LinkedHashMap is still just a HashMap with an insertion history, and that's also how JDK treats it:

var m1 = new LinkedHashMap<String, Integer>();
var m2 = new LinkedHashMap<String, Integer>();

m1.put("a", 1);
m1.put("b", 1);
m2.put("b", 1);
m2.put("a", 1);

System.out.println("m1.equals(m2) = " + m1.equals(m2));
System.out.println("m1.hashCode() = " + m1.hashCode());
System.out.println("m2.hashCode() = " + m2.hashCode());

so a natural course of action is to stay consistent with this approach and use ordered hashing for data structures that rely on the ordering of elements