자바 HashTable

개요[ | ]

배열(버킷)을 사용하여 데이터를 저장하기 때문에 빠른 검색이 가능
배열의 index가 겹치게 될경우 덮어씀
import java.util.Map;  
import java.util.Hashtable;  
class Hashtable1{  
 public static void main(String args[]){  
  Hashtable<Integer,String> hm=new Hashtable<Integer,String>();  
  
  hm.put(100,"철수");  
  hm.put(102,"제타");  
  hm.put(101,"영희");  
  hm.put(103,"허준");  
  
  for(Map.Entry m:hm.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  
 }  
}
  • 제거
import java.util.Hashtable;  

class Hashtable1{  
 public static void main(String args[]){  
  Hashtable<Integer,String> map=new Hashtable<Integer,String>();  
  
  map.put(100,"철수");  
  map.put(102,"제타");  
  map.put(101,"영희");  
  map.put(103,"허준");  
  
     System.out.println("Before remove: "+ map);    
       map.remove(102);  
       System.out.println("After remove: "+ map);  
 }
}
  • getOrDefault 테스트
import java.util.Hashtable;  
class Hashtable1{  
 public static void main(String args[]){  
  Hashtable<Integer,String> map=new Hashtable<Integer,String>();  
  
  map.put(100,"철수");  
  map.put(102,"제타");  
  map.put(101,"영희");  
  map.put(103,"허준");  
  
     System.out.println(map.getOrDefault(101, "Not Found"));  
     System.out.println(map.getOrDefault(105, "Not Found"));  
 }
}
  • putIfAbsent 테스트
import java.util.*;  
class Hashtable1{  
 public static void main(String args[]){  
  Hashtable<Integer,String> map=new Hashtable<Integer,String>();  
  
  map.put(100,"철수");  
  map.put(102,"제타");  
  map.put(101,"영희");  
  map.put(103,"허준");  
  
     System.out.println("Initial Map: "+map);  
     map.putIfAbsent(104,"Gaurav");  
     System.out.println("Updated Map: "+map);  
     map.putIfAbsent(101,"Vijay");  
     System.out.println("Updated Map: "+map);  
 }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}