Map
- Collection 인터페이스와는 다른 저장 방식을 가진다.
- 키(key)와 값(value)으로 구성되어 있으며 키와 값은 모두 인스턴스이다.
(키(key) : 값(value)을 갖기 위한 이름 역할을 하는 객체를 의미)
- 저장순서 유지X
- 키는 중복을 허용하지 않지만 키가 다르면 중복 되는 값은 저장 가능
- HashMap, HashTable, TreeMap 등 대표적인 클래스가 있다.
HashMap
HashMap 인스턴스 생성
HashMap hmap = new HashMap();
//Map hmap2 = new HashMap();
키와 값 쌍으로 저장한다. 키와 값 둘 다 반드시 객체여야 한다.
hmap.put("one", new Date());
hmap.put(12, "red apple");
hmap.put(33, 123);
/* autoBoxing 처리 됨 : int => Integer */
저장 순서 유지하지 않음
키는 중복 저장 되지 않음(set방식) : 최근 값으로 덮어쓰기 put
키가 다르면 값 개체 저장은 중복 가능
System.out.println("hmap : " + hmap);
hmap.put(12, "yellow banana");
System.out.println("hmap : " + hmap);
hmap.put(11, "yellow banana");
System.out.println("hmap : " + hmap);
출력값=>
hmap : {33=123, one=Wed Jan 12 01:26:47 KST 2022, 12=red apple}
hmap : {33=123, one=Wed Jan 12 01:26:47 KST 2022, 12=yellow banana}
hmap : {33=123, one=Wed Jan 12 01:26:47 KST 2022, 11=yellow banana, 12=yellow banana}
값 객체의 내용을 가져올 때
System.out.println("키 11에 대한 객체 : " + hmap.get(11));
//키 11에 대한 객체 : yellow banana
키 값을 가지고 삭제 처리할 때 remove
hmap.remove(11);
System.out.println("hmap : " + hmap);
//hmap : {33=123, one=Wed Jan 12 01:26:47 KST 2022, 12=yellow banana}
저장된 객체 수를 확인할 때 size
System.out.println("hmap에 저장 된 객체 수 : " + hmap.size());
//hmap에 저장된 객체 수 : 3
제네릭 설정한 HashMap 인스턴스 생성
HashMap<String, String> hmap2 = new HashMap<>();
hmap2.put("one", "java");
hmap2.put("two", "oracle");
hmap2.put("three", "jdbc");
hmap2.put("four", "html");
hmap2.put("five", "css");
1. keySet()을 이용해서 키만 따로 set으로 만들고, iterator()로 키에 대한 목록을 만든다.
Iterator<String> keyIter = hmap2.keySet().iterator();
while(keyIter.hasNext()) {
String key = keyIter.next();
String value = hmap2.get(key);
System.out.println(key + " = " + value);
}
출력값=>
four = html
one = java
two = oracle
three = jdbc
five = css
2. 저장된 value 객체들만 values()로 Collection으로 만든다.
Collection<String> values = hmap2.values();
2-1. iterator()로 목록 만들어서 처리
Iterator<String> valueIter = values.iterator();
while(valueIter.hasNext()) {
System.out.println(valueIter.next());
}
출력값=>
html
java
oracle
jdbc
css
2-2. 배열로 만들어서 처리
Object[] valueArr = values.toArray();
for(int i = 0; i < valueArr.length; i++) {
System.out.println(i + " : " + valueArr[i]);
}
0 : html
1 : java
2 : oracle
3 : jdbc
4 : css
3. Map의 내부 클래스인 EntrySet을 이용 : entrySet()
Set<Map.Entry<String, String>> set = hmap2.entrySet();
Iterator<Map.Entry<String, String>> entryIter = set.iterator();
while(entryIter.hasNext()) {
Map.Entry<String, String> entry = entryIter.next();
System.out.println(entry.getKey() +" : " + entry.getValue());
}
four : html
one : java
two : oracle
three : jdbc
five : css
Properties
- 설정 파일의 값을 읽어서 어플리케이션에 적용할 때 사용한다.
- 키와 값을 String타입으로 제한한 Map컬렉션
setProperty() : Properties객체에 해당 key값과 value값 세트로 저장
Properties prop = new Properties();
prop.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
prop.setProperty("url", "jdbc:oracle:thin:@127.0.0.1:1521:xe");
prop.setProperty("user", "student");
prop.setProperty("password", "student");
System.out.println(prop);
출력값=>
{password=student, driver=oracle.jdbc.driver.OracleDriver, user=student, url=jdbc:oracle:thin:@127.0.0.1:1521:xe}
try {
prop.store(new FileOutputStream("driver.dat"), "jdbc driver");
prop.store(new FileWriter("driver.txt"), "jdbc driver");
prop.storeToXML(new FileOutputStream("driver.xml"), "jdbc driver");
} catch (IOException e) {
e.printStackTrace();
}
파일로부터 읽어와서 Properties에 기록
Properties prop2 = new Properties();
try {
//prop2.load(new FileInputStream("driver.dat"));
//prop2.load(new FileReader("driver.txt"));
prop2.loadFromXML(new FileInputStream("driver.xml"));
/* Properties의 모든 키 값 목록을 대상 스트림에 내보내기 한다. */
prop2.list(System.out);
System.out.println(prop2.getProperty("driver"));
System.out.println(prop2.getProperty("url"));
System.out.println(prop2.getProperty("user"));
System.out.println(prop2.getProperty("password"));
} catch (IOException e) {
e.printStackTrace();
}
출력값=>
-- listing properties --
password=student
driver=oracle.jdbc.driver.OracleDriver
user=student
url=jdbc:oracle:thin:@127.0.0.1:1521:xe
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:xe
student
student
properties부분은 전혀 이해 못함.. 다시 강의 들어야함!!
'TIL > Java' 카테고리의 다른 글
[Java] 입출력(IO) - 1. 입력스트림, 출력스트림 (0) | 2022.01.13 |
---|---|
[Java] 예외 처리(Exception) - Throws, try-catch, multi-catch, 사용자정의 Exception, try-with-resource, Exception 오버라이딩 (0) | 2022.01.13 |
[Java] 컬렉션(Collection) - 3. Set(HashSet, LinkedHashSet, TreeSet) (0) | 2022.01.13 |
[Java] 컬렉션(Collection) - 2. List(LinkedList), Stack, Queue (0) | 2022.01.13 |
[Java] 컬렉션(Collection) - 1. List(ArrayList) (0) | 2022.01.13 |