https://github.com/tndus165/JBGW08_023_NHNMART
GitHub - tndus165/JBGW08_023_NHNMART
Contribute to tndus165/JBGW08_023_NHNMART development by creating an account on GitHub.
github.com
HashMap 구현하기
https://nhnacademy.dooray.com/share/pages/9Wgzcqy8TA2Cku9o6Aff-Q/3398899814867293405
14.Collections Framework
nhnacademy.dooray.com

Map = Key/Value을 하나의 쌍으로 묶어서 저장하는 Collection을 구현하는데 사용
HashMap
- HashMap은 Map을 구현한 구현체
- Key를 해싱하여 데이터를 저장하고 꺼내오기 때문에 속도가 빠름
- Key / Value 쌍으로 구성되어 있습니다.
- Key : Collection내에서 유일한 값(Key는 중복될 수 없음) , null값 허용
- Value. : 데이터의 중복을 허용합니다.
- Key/Value의 쌍으로 관리함으로 Iterator를 사용하지 않고 해당 Key에서 데이터를 바로 추출할 수 있습니다.
- 내부적으로 동기화가 (Syncronized)포함되지 않기때문에 HashTable에 비해서 처리속도가 빠름니다.
- List는 순서가 있지만 Map은 순서가 없습니다.

해싱(Hashing) : 키(key) 값을 해시 함수에 대입하여 계산된 결과를 주소로 사용하여 값에 접근하는 방법
HashTable
- HashTable은 Collections Framework 이전부터 존재하던 레거시 class입니다
- 사용하는 방법은 HashMap과 동일합니다.
- Vector처럼 Multi-Thread 환경에서 동작하도록 만들어진 자료구조 입니다.
- 서로다른 Thread가 block / unblock 되는 대기시간을 기다리기 때문에 HashMap에 비해서 속도가 느립니다.
Get Method(쓰레드 동기화)
public synchronized V get(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return (V)e.value;
}
}
return null;
}
EX>
public class Example12 {
public static void main(String[] args) {
Hashtable hashtable = new Hashtable();
hashtable.put("red", "#FF0000");
hashtable.put("white","#FFFFFF");
hashtable.put("blue", "#0000FF");
hashtable.put("green","#00FF00");
Set<String> keySet = hashtable.keySet();
for(String key : keySet){
System.out.println("key:" + key + ":" + hashtable.get(key));
}
}
}
TreeMap
- TreeMap은 이진검색트리의 형태로 검색과 정렬에 특화된 Collection입니다.
- TreeSet 은 단순히 값만 저장한다면 TreeMap 은 key/value 형태의 Entry 를 저장합니다.
- TreeMap은 객체를 저장하는 동시에 키를 기준으로 정렬을 합니다.
(숫자 : 값, 문자 : 유니코드) - TreeMap은 HashMap보다 성능이 떨어집니다.
- 하지만 정렬된 상태로 Map을 유지하거나 정
렬된 데이터를 조회(범위탐색)할 경우 HashMap을 사용하는 것보다 효율성이 좋습니다.
- 하지만 정렬된 상태로 Map을 유지하거나 정
- 데이터를 저장할 때 즉시 정렬하기 때문에 수정/삭제가 HashMap보다 오래 걸립니다.
- TreeMap 은 red-black tree 구성되어 있습니다.
public class Example13 {
public static void main(String[] args) {
TreeMap treeMap = new TreeMap();
treeMap.put(8,"orange");
treeMap.put(2,"green");
treeMap.put(7,"silver");
treeMap.put(3,"white");
treeMap.put(4,"pink");
treeMap.put(10,"yellow");
treeMap.put(5,"blue");
treeMap.put(6,"gray");
treeMap.put(9,"black");
treeMap.put(1,"red");
Set<Map.Entry> entrySet = treeMap.entrySet();
entrySet.forEach(o->{
System.out.println(o.getKey() + " : " + o.getValue() );
});
}
}
Properties
- Properties는 HashTable을 상속받아 구현되었고, String, String 형태로 저장되는 단순한 Collection 입니다.
- Properties를 이용해서 공동으로 사용할 환경변수 들을 저장할 수 있습니다.
public class Exception14 {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("driver","com.mysql.cj.jdbc.driver");
properties.setProperty("username","nhnacademy");
properties.setProperty("password","1234");
properties.setProperty("url","127.0.0.1");
try(OutputStream out = new FileOutputStream("dbinfo.properties")){
properties.store(out,"mysql database info");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
try(InputStream inputStream = new FileInputStream("dbinfo.properties");) {
Properties loadProperties = new Properties();
loadProperties.load(inputStream);
loadProperties.forEach((k,v)->{
System.out.println("key:" + k + "/ value:" + v);
});
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
db.properties
- project root /dbinfo.properties 파일에 저장됩니다.
'✎NHN Academy | JAVA' 카테고리의 다른 글
| NHN Academy - 2024.09.24(Tue) (0) | 2024.09.24 |
|---|---|
| NHN Academy - 2024.09.23(Mon) (0) | 2024.09.23 |
| NHN Academy - 2024.09.11(Wed) (0) | 2024.09.12 |
| NHN Academy - 2024.09.10(Tue) (1) | 2024.09.11 |
| NHN Academy - 2024.09.09(Mon) (4) | 2024.09.09 |