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

(새 문서: ==개요== ; ; ; * <syntaxhighlight lang='java' run> import java.io.*; import java.util.*; class AddElementsToHashtable { public static void main(String args[]) { H...)
 
1번째 줄: 1번째 줄:
==개요==
==개요==
;
;배열(버킷)을 사용하여 데이터를 저장하기 때문에 빠른 검색이 가능
;
;배열의 index가 겹치게 될경우 덮어씀
;
*
*
<syntaxhighlight lang='java' run>
<syntaxhighlight lang='java' run>
import java.io.*;
import java.util.*;
import java.util.*;
class Hashtable1{ 
public static void main(String args[]){ 
  Hashtable<Integer,String> hm=new Hashtable<Integer,String>();
    
    
class AddElementsToHashtable {
  hm.put(100,"철수");
    public static void main(String args[])
  hm.put(102,"제타");
    {
  hm.put(101,"영희");
        Hashtable<Integer, String> ht1 = new Hashtable<>();
  hm.put(103,"허준");
        Hashtable<Integer, String> ht2
            = new Hashtable<Integer, String>();
        ht1.put(1, "one");
        ht1.put(2, "two");
        ht1.put(3, "three");
    
    
        ht2.put(4, "four");
  for(Map.Entry m:hm.entrySet()){ 
        ht2.put(5, "five");
  System.out.println(m.getKey()+" "+m.getValue()); 
        ht2.put(6, "six");
  } 
</syntaxhighlight>
 
* 제거
<syntaxhighlight lang='java' run>
import java.util.*; 
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,"허준");
 
    System.out.println("Before remove: "+ map);   
      map.remove(102); 
      System.out.println("After remove: "+ map); 
</syntaxhighlight>
<syntaxhighlight lang='java' run>
import java.util.*; 
class Hashtable1{ 
public static void main(String args[]){ 
  Hashtable<Integer,String> hm=new Hashtable<Integer,String>(); 
    
    
        System.out.println("Mappings of ht1 : " + ht1);
  hm.put(100,"철수"); 
        System.out.println("Mappings of ht2 : " + ht2);
  hm.put(102,"제타"); 
    }
  hm.put(101,"영희"); 
}
  hm.put(103,"허준"); 
 
    System.out.println(map.getOrDefault(101, "Not Found"));
    System.out.println(map.getOrDefault(105, "Not Found"));
}
</syntaxhighlight>
</syntaxhighlight>
*
*  
*  



2021년 10월 18일 (월) 17:40 판

1 개요

배열(버킷)을 사용하여 데이터를 저장하기 때문에 빠른 검색이 가능
배열의 index가 겹치게 될경우 덮어씀
import java.util.*;  
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.*;  
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,"허준");  
  
     System.out.println("Before remove: "+ map);    
       map.remove(102);  
       System.out.println("After remove: "+ map);  
 }
import java.util.*;  
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,"허준");  
  
     System.out.println(map.getOrDefault(101, "Not Found"));  
     System.out.println(map.getOrDefault(105, "Not Found"));  
 }

2 같이 보기

  • [[ ]]
  • [[ ]]
  • [[ ]]
  • [[ ]]
  • [[ ]]
  • [[ ]]

3 참고

[[분류: ]] [[분류: ]]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}