Dart Map hashCode
Syntax & Examples


Syntax of Map.hashCode

The syntax of Map.hashCode property is:

 int hashCode 

This hashCode property of Map the hash code for this object.

Return Type

Map.hashCode returns value of type int.



✐ Examples

1 Get hash code of a string

In this example,

  1. We create a string named str with the value 'Hello, world!'.
  2. We then access the hashCode property of the string.
  3. We print the hash code to standard output.

Dart Program

void main() {
  String str = 'Hello, world!';
  int hashCode = str.hashCode;
  print('Hash code of str: $hashCode');
}

Output

Hash code of str: 287797974

2 Get hash code of an integer

In this example,

  1. We create an integer named number with the value 42.
  2. We then access the hashCode property of the integer.
  3. We print the hash code to standard output.

Dart Program

void main() {
  int number = 42;
  int hashCode = number.hashCode;
  print('Hash code of number: $hashCode');
}

Output

Hash code of number: 42

3 Get hash code of a list

In this example,

  1. We create a list named list containing strings 'apple', 'banana', and 'cherry'.
  2. We then access the hashCode property of the list.
  3. We print the hash code to standard output.

Dart Program

void main() {
  List<String> list = ['apple', 'banana', 'cherry'];
  int hashCode = list.hashCode;
  print('Hash code of list: $hashCode');
}

Output

Hash code of list: 507029333

Summary

In this Dart tutorial, we learned about hashCode property of Map: the syntax and few working examples with output and detailed explanation for each example.