I just realized that implementing the following algorithm to compute the hash code for a stream is not possible using Stream.reduce(...). The problem is that the initial see
As a first approach I would use the collect-to-a-list solution as long as you don't have performance concerns. That way you avoid reimplementing the wheel and if one day the hash algorithm changes you benefit from that and you are also safe if the stream is parallelized (even if I'm not sure that's a real concern).
The way I would implement it can vary depending on how and when you need to compare your different datastructures (let's call it Foo
).
If you do it manually and sparsly a simple static function may be enough:
public static int computeHash(Foo origin, Collection> selectors) {
return selectors.stream()
.map(f -> f.apply(origin))
.collect(Collectors.toList())
.hashCode();
}
And use it like this
if(computeHash(foo1, selectors) == computeHash(foo2, selectors)) { ... }
However, if instances of Foo
are themselves stored in Collection
and you need both hashCode()
and equals()
(from Object
) to be implemented, I would wrap it inside a FooEqualable
:
public final class FooEqualable {
private final Foo origin;
private final Collection> selectors;
public FooEqualable(Foo origin, Collection> selectors) {
this.origin = origin;
this.selectors = selectors;
}
@Override
public int hashCode() {
return selectors.stream()
.map(f -> f.apply(origin))
.collect(Collectors.toList())
.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FooEqualable) {
FooEqualable that = (FooEqualable) obj;
Object[] a1 = selectors.stream().map(f -> f.apply(this.origin)).toArray();
Object[] a2 = selectors.stream().map(f -> f.apply(that.origin)).toArray();
return Arrays.equals(a1, a2);
}
return false;
}
}
I'm fully aware that this solution isn't optimized (performance-wise) if multiple calls to hashCode()
and equals()
are made but I tend not to optimize except if it becomes a concern.