"자바 HashTable"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 3개는 보이지 않습니다)
4번째 줄: 4번째 줄:
*
*
<syntaxhighlight lang='java' run>
<syntaxhighlight lang='java' run>
import java.util.*;   
import java.util.Map; 
import java.util.Hashtable;   
class Hashtable1{   
class Hashtable1{   
  public static void main(String args[]){   
  public static void main(String args[]){   
23번째 줄: 24번째 줄:
* 제거
* 제거
<syntaxhighlight lang='java' run>
<syntaxhighlight lang='java' run>
import java.util.*;   
import java.util.Hashtable;   
 
class Hashtable1{   
class Hashtable1{   
  public static void main(String args[]){   
  public static void main(String args[]){   
   Hashtable<Integer,String> hm=new Hashtable<Integer,String>();   
   Hashtable<Integer,String> map=new Hashtable<Integer,String>();   
    
    
   hm.put(100,"철수");   
   map.put(100,"철수");   
   hm.put(102,"제타");   
   map.put(102,"제타");   
   hm.put(101,"영희");   
   map.put(101,"영희");   
   hm.put(103,"허준");   
   map.put(103,"허준");   
    
    
     System.out.println("Before remove: "+ map);     
     System.out.println("Before remove: "+ map);     
39번째 줄: 41번째 줄:
}   
}   
</syntaxhighlight>
</syntaxhighlight>
*getOrDefault 테스트
<syntaxhighlight lang='java' run>
<syntaxhighlight lang='java' run>
import java.util.*;   
import java.util.Hashtable;   
class Hashtable1{   
class Hashtable1{   
  public static void main(String args[]){   
  public static void main(String args[]){   
   Hashtable<Integer,String> hm=new Hashtable<Integer,String>();   
   Hashtable<Integer,String> map=new Hashtable<Integer,String>();   
    
    
   hm.put(100,"철수");   
   map.put(100,"철수");   
   hm.put(102,"제타");   
   map.put(102,"제타");   
   hm.put(101,"영희");   
   map.put(101,"영희");   
   hm.put(103,"허준");   
   map.put(103,"허준");   
    
    
     System.out.println(map.getOrDefault(101, "Not Found"));   
     System.out.println(map.getOrDefault(101, "Not Found"));   
55번째 줄: 58번째 줄:
}   
}   
</syntaxhighlight>
</syntaxhighlight>
*  
* putIfAbsent 테스트
 
<syntaxhighlight lang='java' run>
==같이 보기==
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); 
* {{나무위키}}
}
* {{리브레위키}}
}
</syntaxhighlight>


[[분류:   ]]
[[분류:자바 Collection]]
[[분류:  ]]

2021년 10월 18일 (월) 22:06 기준 최신판

개요[ | ]

배열(버킷)을 사용하여 데이터를 저장하기 때문에 빠른 검색이 가능
배열의 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 }}