Java中的List hashCode()方法及其示例
此方法用于生成给定列表的hashCode。
语法:
int hashCode()
参数: 此函数没有参数。
返回值: 此函数返回给定列表的hashCode值。
下面的程序展示了此方法的实现。
程序1:
// Java代码展示了List接口中hashCode方法的实现
import java.util.*;
public class GfG {
// 主函数
public static void main(String[] args)
{
// 初始化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代码展示了List接口中hashCode方法的实现
import java.util.*;
public class GfG {
// 主函数
public static void main(String[] args)
{
// 初始化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
极客教程