티스토리 뷰

반응형
SMALL

 

Hashtable : Object 유형으로 저장할 수 있으며, Key 와 Value 를 매핑하여 저장한다.

Data 가 약 75% 차면 자동으로 저장 영역을 확대 하며, Key 값의 중복을 허용하지 않는다.

또한 불러올 때 순서는 무작위로 불러온다.

 

 

Hashtable 은 메모리상에 존재하기 때문에 I/O 속도가 빠르다.

간단한 작업이라면 DB 에 넣지 말고 Hashtable 을 이용하는것도 나쁘지 않다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package map.hashtable;
 
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;
 
class Member{
    String name;
    String job;
    
    public Member(String name, String job) {
        this.name = name;
        this.job = job;
    }
}
 
public class Test01 {
    public static void main(String[] args) {
        Hashtable<String, Member> ht = new Hashtable<String, Member>();    //객체 생성    Generic 을 Key 와 Value 각각 주어야 한다.
        
        //1. 추가 add()    중복을 허용하지 않음.
        ht.put("유재석"new Member("유재석","개그맨"));
        
        Member gi = new Member("지석진""개그맨");
        ht.put(gi.name, gi);
        
        ht.put("송지효"new Member("송지효""배우"));
        ht.put("하하"new Member("하하""가수"));
        ht.put("이광수"new Member("이광수""모델"));
        ht.put("김종국"new Member("김종국","가수"));
        ht.put("김종국"new Member("김종국","가수"));
        ht.put("김종국"new Member("김종국","가수"));
        
        //2. Size 확인 .size()
        System.out.println("hs의 Size( 삭제 전 ) = "+ht.size());
        
        //3. 검색 .containsKey()
        System.out.print("검색 : ");
        Scanner in = new Scanner(System.in);
        String search = in.next();
        
        if(ht.containsKey(search))    {
            System.out.println("Key 값    : "+ht.get(search).name);
            System.out.println("Value 값 : "+ht.get(search).job);
        }
        else     System.out.println("그런사람 업음");
        
        //4. 삭제
        if(ht.containsKey(search))    {
            ht.remove(search);        //Key 값과 Value 값이 다 지워짐.
            System.out.println("hs의 Size( 삭제 후 ) = "+ht.size());
        }
        else     System.out.println("그런사람 업음");
        
        //5. 전체삭제 clear()
//        ht.clear();
        
        //6. 전체 출력
        Enumeration<String> enu = ht.keys();
//        System.out.println(enu.hasMoreElements()); --> true // 
        while(enu.hasMoreElements()) { //hasNext()
            String key = enu.nextElement();    //next()
            Member save= ht.get(key);            //해당 key 값을 가져와서 Member 에 저장.
            System.out.print(save.name+"-");
            System.out.print(save.job);
            System.out.println();
        }
    }
}
 
cs

 

 

반응형
LIST

'JAVA > 정리' 카테고리의 다른 글

[ Java #2 ] Collection HashSet  (0) 2017.09.14
[ Java #1 ] Collection & Iterator  (0) 2017.09.14
댓글