Java List hashCode()方法及实例

Java List hashCode()方法及实例

该方法用于为给定的列表生成hashCode。

语法

int hashCode()
Java

参数: 此函数没有参数。

返回: 该函数返回给定列表的hashCode值。

下面的程序显示了这个方法的实现。

程序1:

// Java code to show the implementation of
// hashCode method in list interface
import java.util.*;
public class GfG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Initializing a list of type Linkedlist
        List<Integer> l = new LinkedList<>();
        l.add(10);
        l.add(15);
        l.add(20);
        System.out.println(l);
  
        int hash = l.hashCode();
  
        System.out.println(hash);
    }
}
Java

输出:

[10, 15, 20]
39886
Java

程序2: 下面是使用Linkedlist实现list.hashCode()的代码。

// Java code to show the implementation of
// hashCode method in list interface
import java.util.*;
public class GfG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Initializing a list of type Linkedlist
        List<String> l = new LinkedList<>();
        l.add("10");
        l.add("15");
        l.add("20");
        System.out.println(l);
  
        int hash = l.hashCode();
  
        System.out.println(hash);
    }
}
Java

输出:

[10, 15, 20]
1586008
Java

参考资料:
Oracle文档

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册