Java教程

Java EnumMap hashCode()

Java EnumMap类的 hashCode()方法用于获取EnumMap的哈希码值。返回一个整数,其值是映射中每个条目的哈希码之和。

语法

public int hashCode()

参数

NA

返回

此方法返回此映射的哈希码值。

异常

NA

示例1

import java.util.*;
public class EnumMapHashCodeExample1 {
    public enum Tutorial{
    CSS, Python, PHP, Java, Javascript
  };
    public static void main(String[] args) {
        EnumMap<Tutorial,String> map = new EnumMap<Tutorial,String>(Tutorial.class);
        map.put(Tutorial.CSS, "1");
        map.put(Tutorial.Python, "2");
        map.put(Tutorial.PHP,"3");
        map.put(Tutorial.Java, "4");
        System.out.println("Hash code value of the map: "+map.hashCode());
        map.clear();
        System.out.println("Hash code value after clearing the map: "+map.hashCode());
    }
}
输出:
Hash code value of the map: -450357866
Hash code value after clearing the map: 0

示例2

import java.util.*;
public class EnumMapHashCodeExample2 {
    public enum Days {
    Monday, Tuesday, Wednesday, Thursday
  };
    public static void main(String[] args) {
        EnumMap<Days, String> map= new EnumMap<Days, String>(Days.class);
        map.put(Days.Monday,"1");
        map.put(Days.Tuesday,"2");
        map.put(Days.Wednesday,"3");
        System.out.println("Hash code value of the map: "+map.hashCode());
        map.clear();
        System.out.println("Hash code value after clearing the map: "+map.hashCode());
    }
}
输出:
Hash code value of the map: -1315471824
Hash code value after clearing the map: 0
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4