📜  Java中的 AbstractList hashCode() 方法及示例

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

Java中的 AbstractList hashCode() 方法及示例

Java.util.AbstractList类的hashCode()方法用于返回此列表的哈希码值。

句法:

public int hashCode()

返回值:此方法返回此列表的哈希码值。

下面是说明hashCode()方法的示例。

示例 1:

// Java program to demonstrate
// hashCode() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : " + arrlist1);
  
            // getting the value at the index 3
            // using get() method
            int code = arrlist1.hashCode();
  
            // print the value
            System.out.println("HashCode : " + code);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
ArrayListlist : [10, 20, 30, 40, 50]
HashCode : 38490301

示例 2:

// Java program to demonstrate
// hashCode() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("C");
            arrlist1.add("D");
            arrlist1.add("E");
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // getting the value at the index 3
            // using get() method
            int code = arrlist1.hashCode();
  
            // print the value
            System.out.println("HashCode : " + code);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
ArrayListlist : [A, B, C, D, E]
HashCode : 90690786