Java OptionalDouble hashCode()方法及示例
hashCode() 方法可以帮助我们获得值的散列代码,如果存在Double值,则为0(零),如果OptionalDouble对象中不存在Double值,则为0。
语法
public int hashCode()
参数: 该方法不接受任何参数。
返回值: 该方法返回当前值的哈希代码值,如果没有值则返回0。以下程序说明了hashCode()方法:
程序1 :
// Java program to demonstrate
// OptionalDouble.hashCode() method
import java.util.OptionalDouble;
public class GFG {
public static void main(String[] args)
{
// create a OptionalDouble instance
OptionalDouble opDouble
= OptionalDouble.of(253255.432525);
System.out.println("OptionalDouble: "
+ opDouble.toString());
// get hashCode value using hashCode()
System.out.println("HashCode value: "
+ opDouble.hashCode());
}
}
输出
OptionalDouble: OptionalDouble[253255.432525]
HashCode value: 885080309
程序2
// Java program to demonstrate
// OptionalDouble.hashCode() method
import java.util.OptionalDouble;
public class GFG {
public static void main(String[] args)
{
// create a OptionalDouble instance
OptionalDouble opDouble
= OptionalDouble.empty();
System.out.println("OptionalDouble: "
+ opDouble.toString());
// get hashCode value using hashCode()
System.out.println("HashCode value: "
+ opDouble.hashCode());
}
}
输出
OptionalDouble: OptionalDouble.empty
HashCode value: 0
参考文献: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalDouble.html#hashCode()