JAVA chapter15. 컬렉션 프레임워크. 15.4 Map 컬렉션
JAVA/CONCEPT 2018. 1. 15. 16:15 |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 | 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<String, String> map = new Hashtable<String, String>(); 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 |
'JAVA > CONCEPT' 카테고리의 다른 글
JAVA chapter15. 컬렉션 프레임워크. 15.6 LIFO와 FIFO 컬렉션 (0) | 2018.01.15 |
---|---|
JAVA chapter15. 컬렉션 프레임워크. 15.5 검색 기능을 강화시킨 컬렉션 // 추가 1:06 (0) | 2018.01.15 |
JAVA chapter15. 컬렉션 프레임워크. 15.3 Set 컬렉션 (0) | 2018.01.15 |
JAVA chapter15. 컬렉션 프레임워크. 15.2 List 컬렉션 (0) | 2018.01.15 |
JAVA chapter15. 컬렉션 프레임워크. 15.1 컬렉션 프레임워크 소개 (0) | 2018.01.15 |