Skip to content
Related Articles

Related Articles

Improve Article

Java Guava | Chars.hashCode() method with Examples

  • Last Updated : 29 Jan, 2019

Chars.hashCode() is a method of Chars Class in Guava Library which is used to return a hash code for a char value. The hashCode is an unique integer value that is calculated by the compiler for an object. It remain the same if the object value does not changes.

Syntax:

public static int hashCode(char value)

Parameter: This method takes a mandatory parameter value which is a char value for which the hashCode is to be found.

Return Value: This method returns an integer value which is the hash code for the specified value.

Below programs illustrate the use of the above method:



Example 1:




// Java code to show implementation of
// Guava's Chars.hashCode() method
  
import com.google.common.primitives.Chars;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Using Chars.hashCode() method to
        // get the hash code for value
        System.out.println("HashCode value of y: "
                           + Chars.hashCode('y'));
    }
}
Output:
HashCode value of y: 121

Example 2:




// Java code to show implementation of
// Guava's Chars.hashCode() method
  
import com.google.common.primitives.Chars;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Using Chars.hashCode() method to
        // get the hash code for value
        System.out.println("HashCode value of J: "
                           + Chars.hashCode('J'));
    }
}
Output:
HashCode value of J: 74

Reference: https://google.github.io/guava/releases/23.0/api/docs/com/google/common/primitives/Chars.html#hashCode-char-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up