Java Vector hashCode() 方法



描述

Java Vector hashCode() 方法用於返回此向量的雜湊碼值。

宣告

以下是 java.util.Vector.hashCode() 方法的宣告

public int hashCode()

引數

返回值

方法呼叫返回此列表的雜湊碼值(int)。

異常

獲取整數向量雜湊碼示例

以下示例演示瞭如何使用 Java Vector hashCode() 方法獲取此向量的雜湊碼。我們使用 add() 方法呼叫每個元素將幾個整數新增到 Vector 物件中,並使用 hashCode() 方法獲取雜湊碼並列印它。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Integer> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);

      // let us get the hashcode of the vector
      System.out.println("Hash code: "+vec.hashCode());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hash code: 1045631

獲取字串向量雜湊碼示例

以下示例演示瞭如何使用 Java Vector hashCode() 方法獲取此向量的雜湊碼。我們使用 add() 方法呼叫每個元素將幾個字串新增到 Vector 物件中,並使用 hashCode() 方法獲取雜湊碼並列印它。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<String> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add("Welcome");
      vec.add("To");
      vec.add("Tutorialspoint");

      // let us get the hashcode of the vector
      System.out.println("Hash code: "+vec.hashCode());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hash code: -1700837919

獲取物件向量雜湊碼示例

以下示例演示瞭如何使用 Java Vector hashCode() 方法獲取此向量的雜湊碼。我們使用 add() 方法呼叫每個元素將幾個 Student 物件新增到 Vector 物件中,並使用 hashCode() 方法獲取雜湊碼並列印它。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Student> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add(new Student(1, "Julie"));
      vec.add(new Student(2, "Robert"));
      vec.add(new Student(3, "Adam"));

      // let us get the hashcode of the vector
      System.out.println("Hash code: "+vec.hashCode());
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hash code: 368956094
java_util_vector.htm
廣告