Java List hashCode()方法及实例
该方法用于为给定的列表生成hashCode。
语法
int hashCode()
参数: 此函数没有参数。
返回: 该函数返回给定列表的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);
}
}
输出:
[10, 15, 20]
39886
程序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);
}
}
输出:
[10, 15, 20]
1586008
参考资料:
Oracle文档
极客教程