JAVA chapter15. 컬렉션 프레임워크


15.4 Map 컬렉션




15.4.1 HashMap


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
package sec04.exam01_hashmap;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
public class HashMapExample1 {
    public static void main(String[] args) {
        //Map 컬렉션 생성
        Map<String, Integer> map = new HashMap<String, Integer>();
        
        //객체 저장
        map.put("신용권"85);
        map.put("홍길동"90);// 키가 같기 때문에 제일 마지막에 저장한 값으로 대체
        map.put("동장군"80);
        map.put("홍길동"95);
        System.out.println("총 Entry 수: " + map.size());// 저장된 총 Entry 수 얻기
        
        //객체 찾기        // 이름(키)으로 점수(값)를 검색
        System.out.println("\t홍길동 : " + map.get("홍길동"));
        System.out.println();
        
        //객체를 하나씩 처리
        Set<String> keySet = map.keySet(); // Key Set 얻기
        Iterator<String> keyIterator = keySet.iterator();// 반복해서 키를 얻고 값을 Map에서 얻어냄
        while(keyIterator.hasNext()) {
          String key = keyIterator.next();
          Integer value = map.get(key);
          System.out.println("\t" + key + " : " + value);
        }        
        System.out.println();    
        
        //객체 삭제
        map.remove("홍길동");
        System.out.println("총 Entry 수: " + map.size());
        
        //객체를 하나씩 처리
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        Iterator<Map.Entry<String, Integer>> entryIterator = entrySet.iterator();
        while(entryIterator.hasNext()) {
          Map.Entry<String, Integer> entry = entryIterator.next();
          String key = entry.getKey();
          Integer value = entry.getValue();
          System.out.println("\t" + key + " : " + value);
        }
        System.out.println();
        
        //객체 전체 삭제
        map.clear();
        System.out.println("총 Entry 수: " + map.size());
    }
}
 
 
cs

 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package sec04.exam01_hashmap;
 
public class Student {
    public int sno;
    public String name;
    
    public Student(int sno, String name) {
        this.sno = sno;
        this.name = name;
    }
 
    public boolean equals(Object obj) { // 학번과 이름이 동일한 경우 true를 리턴
        if(obj instanceof Student) {
            Student student = (Student) obj;
            return (sno==student.sno) && (name.equals(student.name)) ;
        } else {
            return false;
        }
    }
 
    public int hashCode() { // 학번과 이름이 같다면 동일한 값 리턴
        return sno + name.hashCode(); // return Object.hash(sno, name);
    }
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package sec04.exam01_hashmap;
 
import java.util.*;
 
public class HashMapExample2 {
    public static void main(String[] args) {
        Map<Student, Integer> map = new HashMap<Student, Integer>();
        
        map.put(new Student(1"홍길동"), 95);
        map.put(new Student(1"홍길동"), 90);
        
        System.out.println("총 Entry 수: " + map.size());
        System.out.println(map.get(new Student(1,"홍길동")));
    }
}
cs



15.4.2 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
package sec04.exam02_hashtable;
 
import java.util.*;
 
public class HashtableExample {
    public static void main(String[] args) {
        Map<StringString> map = new Hashtable<StringString>();
 
        map.put("spring""12");
        map.put("summer""123");
        map.put("fall""1234");
        map.put("winter""12345");
        
        Scanner scanner = new Scanner(System.in); // 키보드로부터 입력된 내용을 받기 위해 생성
        
        while(true) {
            System.out.println("아이디와 비밀번호를 입력해주세요");
            System.out.print("아이디: ");
            String id = scanner.nextLine(); // 키보드로 입력한 아이디를 읽는다
            
            System.out.print("비밀번호: ");
            String password = scanner.nextLine(); // 키보드로 입력한 비밀번호를 읽는다
            System.out.println();
            
            if(map.containsKey(id)) { // 아이디인 키가 존재하는지 확인한다
                if(map.get(id).equals(password)) { // 비밀번호를 비교한다
                    System.out.println("로그인 되었습니다");
                    break;
                } else {
                    System.out.println("비밀번호가 일치하지 않습니다.");
                }                
            } else {
                System.out.println("입력하신 아이디가 존재하지 않습니다");
            }
        }
    }
}
cs



15.4.3 Properties




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package sec04.exam03_properties;
 
import java.io.FileReader;
import java.net.URLDecoder;
import java.util.Properties;
 
public class PropertiesExample {
    public static void main(String[] args) throws Exception {
        Properties properties  = new Properties();
        String path = PropertiesExample.class.getResource("database.properties").getPath();
        path = URLDecoder.decode(path, "utf-8"); // 경로에 한글이 있는 경우 한글을 복원
        properties.load(new FileReader(path));
        
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password"); 
        
        System.out.println("driver : " + driver);
        System.out.println("url : " + url);
        System.out.println("username : " + username);
        System.out.println("password : " + password);
    }
}
cs

database.properties
1
2
3
4
driver=oracle.jdbc.OracleDirver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=tiger
cs




Posted by 너래쟁이
: