Java EnumMap hashCode()

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

1 语法

public int hashCode()  

2 参数

3 返回值

返回EnumMap的哈希码值。

4 EnumMap hashCode()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java EnumMap.hashCode的例子
 */
import java.util.*;

public class Demo {

    // 创建枚举
    public enum Tutorial{
        CSS, Python, PHP, Java, Javascript
    };
    public static void main(String[] args) {
        //创建EnumMap
        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");
        //打印hashcode
        System.out.println("Hash code value of the map: "+map.hashCode());
        //清空Map
        map.clear();
        //打印hashcode
        System.out.println("Hash code value after clearing the map: "+map.hashCode());
    }
}

输出结果为:

Hash code value of the map: -224382088
Hash code value after clearing the map: 0

5 EnumMap hashCode()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java EnumMap.hashCode的例子
 */
import java.util.*;

public class Demo {

    // 创建枚举
    public enum Days {
        Monday, Tuesday, Wednesday, Thursday
    };
    public static void main(String[] args) {
        //创建EnumMap
        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");
        //打印HashCode
        System.out.println("Hash code value of the map: "+map.hashCode());
        //清空map
        map.clear();
        //打印HashCode
        System.out.println("Hash code value after clearing the map: "+map.hashCode());
    }
}

输出结果为:

Hash code value of the map: -246067737
Hash code value after clearing the map: 0

热门文章

优秀文章