Dart String hashCode
Syntax & Examples
Syntax of String.hashCode
The syntax of String.hashCode property is:
int hashCode This hashCode property of String returns a hash code derived from the code units of the string.
Return Type
String.hashCode returns value of type int.
✐ Examples
1 Calculate hash code for a string
In this example,
- We create a string
strwith the value 'Hello'. - We then access the
hashCodeproperty of the string. - The hash code of the string is stored in
hashCode. - We print the hash code to standard output.
Dart Program
void main() {
String str = 'Hello';
int hashCode = str.hashCode;
print('Hash code of $str: $hashCode');
}Output
Hash code of Hello: 180399535
2 Calculate hash code for another string
In this example,
- We create a string
strwith the value 'ABCDEF'. - We then access the
hashCodeproperty of the string. - The hash code of the string is stored in
hashCode. - We print the hash code to standard output.
Dart Program
void main() {
String str = 'ABCDEF';
int hashCode = str.hashCode;
print('Hash code of $str: $hashCode');
}Output
Hash code of ABCDEF: 463689234
3 Calculate hash code for a different string
In this example,
- We create a string
strwith the value 'Lorem ipsum'. - We then access the
hashCodeproperty of the string. - The hash code of the string is stored in
hashCode. - We print the hash code to standard output.
Dart Program
void main() {
String str = 'Lorem ipsum';
int hashCode = str.hashCode;
print('Hash code of $str: $hashCode');
}Output
Hash code of Lorem ipsum: 21565321
Summary
In this Dart tutorial, we learned about hashCode property of String: the syntax and few working examples with output and detailed explanation for each example.