📜  Java中的堆栈hashCode()方法与示例

📅  最后修改于: 2022-05-13 01:55:34.904000             🧑  作者: Mango

Java中的堆栈hashCode()方法与示例

Java中的Java Java ()方法用于获取这个Stack的hashcode值。

句法:

Stack.hashCode()

参数:该方法不带任何参数。

返回值:该方法返回该Stack的哈希码值,为Integer类型。

下面的程序说明了Java.util.Stack.hashCode() 方法:

程序 1:与字符串元素堆叠。

// Java code to illustrate hashCode()
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack stack = new Stack();
  
        // Use add() method to add elements into the Stack
        stack.add("Welcome");
        stack.add("To");
        stack.add("Geeks");
        stack.add("4");
        stack.add("Geeks");
  
        // Displaying the Stack
        System.out.println("Stack: " + stack);
  
        // Displaying the hashCode value of Stack
        System.out.println("The hashCode value is: "
                           + stack.hashCode());
    }
}
输出:
Stack: [Welcome, To, Geeks, 4, Geeks]
The hashCode value is: -878886256

程序 2:与整数元素堆叠。

// Java code to illustrate hashCode()
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack stack = new Stack();
  
        // Use add() method to add elements into the Stack
        stack.add(10);
        stack.add(20);
        stack.add(30);
        stack.add(40);
        stack.add(50);
  
        // Displaying the Stack
        System.out.println("Stack: " + stack);
  
        // Displaying the hashCode value of Stack
        System.out.println("The hashCode value is: "
                           + stack.hashCode());
    }
}
输出:
Stack: [10, 20, 30, 40, 50]
The hashCode value is: 38490301