Java中HashSet的hashCode()方法用于获取此HashSet实例的hashCode值。它返回一个整数值,该值是此HashSet实例的hashCode值。
用法:
public int hashCode()
参数:此功能没有参数。
返回值:该方法返回一个整数值,该值是此HashSet实例的hashCode值。
下面的示例说明了HashSet.hashCode()方法:
示例1:
// Java code to demonstrate the working of
// hashCode() method in HashSet
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// creating an HashSet
HashSet arr
= new HashSet();
// using add() to initialize values
// [1, 2, 3, 4]
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
// print HashSet
System.out.println("HashSet: "
+ arr);
// Get the hashCode value
// using hashCode() value
System.out.println("HashCode value: "
+ arr.hashCode());
}
}
输出:
HashSet: [1, 2, 3, 4]
HashCode value: 10
示例2:
// Java code to demonstrate the working of
// hashCode() method in HashSet
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// creating an HashSet
HashSet arr
= new HashSet();
// using add() to initialize values
// [Geeks, For, ForGeeks, GeeksForGeeks]
arr.add("Geeks");
arr.add("For");
arr.add("ForGeeks");
arr.add("GeeksForGeeks");
// print HashSet
System.out.println("HashSet: "
+ arr);
// Get the hashCode value
// using hashCode() value
System.out.println("HashCode value: "
+ arr.hashCode());
}
}
输出:
HashSet: [ForGeeks, Geeks, For, GeeksForGeeks]
HashCode value: -482506029