Java教程

Java Vector hashCode()

Java Vector类的 hashCode()方法用于获取位于以下位置的向量的哈希码值

语法:

以下是 hashCode()方法的声明:
public int hashCode()

参数:

此方法不接受任何参数。

返回:

hashCode()方法返回此向量的哈希码值。

例外:

NA

兼容版本:

Java 1.2及更高版本

示例1:

import java.util.*;
public class VectorHashCodeExample1 {  
    public static void main(String arg[]) {   
        //Create an empty vector
        Vector<Integer> vec = new Vector<>();
        //Add element in the vector
        vec.add(1);
        vec.add(-2);
        vec.add(3);       
        vec.add(-4);
        vec.add(5);
        //Printing the element
        System.out.println("Element in vector = "+vec);
        //Get the hashcode for this vector
        System.out.println("HashCode vector = "+vec.hashCode());   
        }            
}
输出:
Element in vector = [1, -2, 3, -4, 5]
HashCode vector = 29495854

示例2:

import java.util.*;
public class VectorHashCodeExample2 {  
    public static void main(String arg[]) {   
        //Create an empty vector
        Vector < String > colors = new Vector < String > ();
        //Add elements in the vector
          colors.add("White");
          colors.add("Green");
          colors.add("Black");
          colors.add("Pink");
          //Print the elements of this vector
          System.out.println("Color elements in vector: " +colors);
          //Get the hash code value for this vector
          System.out.println("HashCode: " +colors.hashCode()); 
        }            
}
输出:
Color elements in vector: [White, Green, Black, Pink]
HashCode: 1877005970

示例3:

import java.util.*;
public class VectorHashCodeExample3 {  
    public static void main(String arg[]) {   
        //Create an empty vector
        Vector<String> vec = new Vector<>();
        //Add element in the vector
        vec.add("@");
        System.out.println(vec.hashCode());
        vec.add("%");
        System.out.println(vec.hashCode());
        vec.add("=");
        System.out.println(vec.hashCode()); 
        }            
}
输出:
95
2982
92503

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4