Skip to content

Uint8List.hashCode #16335

Closed
Closed

Description

@DartBot

This issue was originally filed by @stevenroose


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?

Activity

  1. rakudrama commented on Jan 27, 2014

    @rakudrama
    Member

    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.

  2. DartBot commented on Jan 27, 2014

    @DartBot
    Author

    This comment was originally written by @stevenroose


    Hmm, they are indeed not immutable, should've seen that.

    Is there a workaround like in Java?

  3. lrhn commented on Jan 28, 2014

    @lrhn
    Member

    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);
     }

  4. lrhn commented on Jan 29, 2014

    @lrhn
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.closed-as-intendedClosed as the reported issue is expected behaviorlibrary-typed-data

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions