Dart Set hashCode
Syntax & Examples


Set.hashCode property

The `hashCode` property in Dart returns the hash code for the Set object.


Syntax of Set.hashCode

The syntax of Set.hashCode property is:

 int hashCode 

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

Return Type

Set.hashCode returns value of type int.



✐ Examples

1 Getting the hash code of a Set of strings

In this example,

  1. We create a Set called `fruits` containing strings.
  2. We use the `hashCode` property to get the hash code of the `fruits` Set.
  3. We then print the hash code to standard output.

Dart Program

void main() {
  Set<String> fruits = {'apple', 'banana', 'cherry'};
  int hash = fruits.hashCode;
  print('Hash code of the Set: $hash');
}

Output

Hash code of the Set: 990880907

2 Getting the hash code of a Set of integers

In this example,

  1. We create a Set called `numbers` containing integers.
  2. We use the `hashCode` property to get the hash code of the `numbers` Set.
  3. We then print the hash code to standard output.

Dart Program

void main() {
  Set<int> numbers = {1, 2, 3, 4, 5};
  int hash = numbers.hashCode;
  print('Hash code of the Set: $hash');
}

Output

Hash code of the Set: 233847813

Summary

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