当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java EnumMap hashCode()用法及代码示例


Java EnumMap 类的hashCode() 方法用于获取EnumMap 的hashcode 值。该方法返回一个整数,其值为map 中每个条目的hash 码之和。

用法

public <strong>int</strong> hashCode()

参数

NA

返回

此方法返回此Map的哈希码值。

异常

NA

例子1

import java.util.*;  
public class EnumMapHashCodeExample1 {  
   // create an enum  
  public enum Tutorial{
  CSS, Python, PHP, Java, Javascript
  }; 
  public static void main(String[] args) {
  //create and populate enum
  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");
  //print the hash code value of the map
  System.out.println("Hash code value of the map:"+map.hashCode()); 	
  //clear the map
  map.clear();	      
  // print the hash code value of the map again 
   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 {      
  // create an enum  
  public enum Days {  
  Monday, Tuesday, Wednesday, Thursday  
  };  
  public static void main(String[] args) {  
  //create and populate enum map  
  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");     
  //print hash code value of the map
  System.out.println("Hash code value of the map:"+map.hashCode()); 	
  //clear the map
  map.clear();	      
  // print hash code value of the map again 
  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



相关用法


注:本文由纯净天空筛选整理自 Java EnumMap hashCode() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。