當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。