You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What steps will reproduce the problem?
Uint8List a = new Uint8List.fromList([0,0]);
Uint8List b = new Uint8List.fromList([0,0]);
print(a.hashCode == b.hashCode);
print(a == b);
What is the expected output?
true
true
What do you see instead?
false
false
Please provide any additional information below.
Java has a method java.util.Arrays.hashcode(byte[])
Why isn't this behaviour adopted? Uint8List resembles an immutable byte array, shouldn't it be comparable and have a hashCode?
You can use package:collection/equality to get an external equality and hash code for a list.
Example:
import "package:collection/equality.dart";
import "dart:collection" show HashMap;
import "dart:typed_data";
main() {
const equality = const ListEquality<int>();
Uint8List a = new Uint8List.fromList([0,0]);
Uint8List b = new Uint8List.fromList([0,0]);
print(equality.hash(a) == equality.hash(b));
print(equality.equals(a, b));
// and use it as parameters to hashmap:
var map = new HashMap(equals: equality.equals, hashCode: equality.hash);
}
We don't plan to change equality or hashCode on collections at this point.
It would be a breaking change for any use of them that assumes the current (identity) equality.
Activity
rakudrama commentedon Jan 27, 2014
Uint8List is using object identity for 'get hashCode' and 'operator =='.
This is consistent with all collections in the dart libraries.
Unlike a String, Uint8List is not immutable:
Uint8List a = new Uint8List.fromList([0,0]);
print(a) -> [0, 0];
a[1] = 2;
print(a) -> [0, 2];
A default hashCode that must look at all the elements of the list at each call would be slow.
If you want to use a Uint8List object as a HashMap key, you can create a HashMap with custom equals and hashCode methods:
https://api.dartlang.org/docs/channels/stable/latest/dart_collection/HashMap.html#
Added Area-Library, Library-TypedData, Triaged labels.
DartBot commentedon Jan 27, 2014
This comment was originally written by @stevenroose
Hmm, they are indeed not immutable, should've seen that.
Is there a workaround like in Java?
lrhn commentedon Jan 28, 2014
You can use package:collection/equality to get an external equality and hash code for a list.
Example:
import "package:collection/equality.dart";
import "dart:collection" show HashMap;
import "dart:typed_data";
main() {
const equality = const ListEquality<int>();
Uint8List a = new Uint8List.fromList([0,0]);
Uint8List b = new Uint8List.fromList([0,0]);
print(equality.hash(a) == equality.hash(b));
print(equality.equals(a, b));
// and use it as parameters to hashmap:
var map = new HashMap(equals: equality.equals, hashCode: equality.hash);
}
lrhn commentedon Jan 29, 2014
We don't plan to change equality or hashCode on collections at this point.
It would be a breaking change for any use of them that assumes the current (identity) equality.
Added AsDesigned label.