package ex0802;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
 - Map
 	: 키와 값 구조
 	: 키는 중복을 허용하지 않음
 	: 키는 순서가 없음
 	: 반복자가 없음(키는 반복자가 존재)
 - Map 구현 클래스
 	: HashMap - 동기화 지원하지 않음
 	: Hashtable - 동기화 지원
 	: TreeMap - 키 순서로 정렬. 키는 Comparable 인터페이스가 구현되어 있어야 함.
 */
public class Ex03_Map {
	public static void main(String[] args) {
		// 키, 값
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		// map에 값 저장
		map.put("서울", 1000);
		map.put("부산", 350);
		map.put("대구", 250);
		map.put("인천", 350);
		map.put("광주", 150);
		map.put("대전", 150);
		map.put("울산", 110);
		map.put("세종", 20);
		map.put("서울", 980); // 동일한 키는 기존 값을 덮어씀
		System.out.println(map);
		
		// map에서 값 가져오기
		int n = map.get("서울");
		System.out.println(n);
		
		boolean b = map.containsKey("서울");
		System.out.println("키에 서울이 존재합니까 ? " + b);
		b = map.containsValue(350);
		System.out.println("값으로 350이 존재합니까 ? "+ b);
		
		System.out.println("map의 전체 데이터 개수 : " + map.size());
		
		map.remove("세종"); // 키가 세종인 데이터 삭제
		System.out.println(map);
		
		// Map은 Iterator가 존재하지 않음. 향상된 for 문도 사용 불가
		// 키는 처음부터 끝까지 순회가능(Iterator가 존재)
		Set<String> keySet = map.keySet(); // 키에서 Set 객체를 반환
		Iterator<String> it = keySet.iterator();
		while(it.hasNext()) {
			String key = it.next();
			Integer value = map.get(key);
			System.out.println(key + " -> " + value);
		}
		
	}
}

 

package ex0802;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Ex04_Map {

	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap<String, Integer>();

		map.put("자바", 80);
		map.put("오라클", 90);
		map.put("빅데이터", 100);
		map.put("서블릿", 60);
		map.put("스프링", 80);

		// map의 키를 Set으로 가져오기
		System.out.println("map의 키를 Set으로 가져오기");
		Set<String> set = map.keySet();
		System.out.println(set);

		System.out.println("map의 값을 List로 가져오기");
		List<Integer> list = new LinkedList<Integer>(map.values());
		System.out.println(list);

		System.out.println("\nmap 전체 리스트 -1");
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			int value = map.get(key);
			System.out.println(key + " -> " + value);
		}

		System.out.println("\nmap 전체 리스트 -2");
		for (String key : map.keySet()) {
			int value = map.get(key);
			System.out.println(key + " -> " + value);
		}
		
		
		
	}

}

package ex0802;

import java.util.Map;
import java.util.TreeMap;

public class Ex05_Map {

	public static void main(String[] args) {
		// TreeMap : 키로 정렬하여 저장. 키는 Comparable 인터페이스가 구현되어 있어야 함.
		// Comparable 인터페이스 : 정렬의 기준
		// Map<String, Integer> map = new TreeMap<>();
		TreeMap<String, Integer> map = new TreeMap<>();
		map.put("서울", 1000);
		map.put("부산", 350);
		map.put("대구", 250);
		map.put("인천", 350);
		map.put("광주", 150);
		map.put("대전", 150);
		map.put("울산", 110);
		map.put("세종", 20);
		
		System.out.println(map);
		
		Map<String, Integer> subMap = map.subMap("대전", "세종");
		System.out.println(subMap); // 대전에서 세종 전까지 추출
	}

}

 

'쌍용강북교육센터 > 8월' 카테고리의 다른 글

0805_Oracle : 단일행함수  (1) 2021.08.05
0803_Oracle : SQL  (1) 2021.08.04
0804_Oracle : SQL  (1) 2021.08.04
0802_Collections  (1) 2021.08.03
0802_Ex01~Ex02_Sort  (1) 2021.08.03

+ Recent posts