Collections - Map

jinkyung·2021년 1월 15일
0

JAVA2

목록 보기
29/35

Map

: key , value를 가짐

package ex05.collections.map13;

import java.util.HashMap;

//Set : Value를 Hash화하여 정렬 후 저장 : 검색에 특화
//Map : Key, Value중에 Key를 Hash화하여 정렬 후 저장 : Key로 Value를 검색(특화)

public class HashMapEx {
	public static void main(String[] args) {
		HashMap<Integer, String> datas = 
				new HashMap<Integer, String>();
		datas.put(3, "금괴 1톤");
		datas.put(5, "1억 달러");
		datas.put(6, "다이어몬드 1000캐럿");
		datas.put(7, "사과 20박스");
		
		System.out.println(datas.get(3));
		
		if(datas.containsKey(5))				//어떤 key값이 존재하는지 
			System.out.println("5번 있다");
		
		if(datas.containsValue("1억 달러")) 		//어떤 value가 존재하는지 
			System.out.println("1억 달러 있다");
		
		
		datas.put(5, "배 100박스 ");			//key 값은 중복 안되므로 기존 key 5에 업데이트.
		System.out.println(datas.get(5));
		
		
	}
}

package ex05.collections.map14;

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

//Set : Value를 Hash화하여 정렬 후 저장 : 검색에 특화
//Map : Key, Value중에 Key를 Hash화하여 정렬 후 저장 : Key로 Value를 검색(특화)

public class HashMapEx {
	public static void main(String[] args) {
		HashMap<Integer, String> datas = 
				new HashMap<Integer, String>();
		datas.put(3, "금괴 1톤");
		datas.put(5, "1억 달러");
		datas.put(6, "다이어몬드 1000캐럿");
		datas.put(7, "사과 20박스");
		
		Set<Integer> keySet = datas.keySet();		//key값만 뽑아내기 
		Iterator<Integer> itr = keySet.iterator();
		while(itr.hasNext()) {
			int key = itr.next();
			String value = datas.get(key);          //value값 출력 
			System.out.println("" + "key : " + key + " = val : " + value );
		}
	}
}

-작곡가 예제

package ex05.collections.map15;

public class ComposerKey {
	private String name;
	private String nation;

	public ComposerKey setName(String name) {
		this.name = name;
		return this;
	}

	public ComposerKey setNation(String nation) {
		this.nation = nation;
		return this;
	}
	
	public ComposerKey() {
		
	}
	
	public ComposerKey(String name, String nation) {
		this.name = name;
		this.nation = nation;
	}

	@Override
	public int hashCode() {
		return this.toString().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		ComposerKey key = (ComposerKey)obj;
		return name.equals(key.name) &&
				nation.equals(key.nation);
	}

	@Override
	public String toString() {
		return name+":"+nation;
	}
}

package ex05.collections.map15;

import java.util.LinkedList;

class Works{
	private String type;
	private String title;
	private String year;	
	
	public Works setType(String type) {
		this.type = type;
		return this;
	}

	public Works setTitle(String title) {
		this.title = title;
		return this;
	}

	public Works setYear(String year) {
		this.year = year;
		return this;
	}

	Works(){
		
	}

	Works(String type, String title, String year){
		this.type = type;
		this.title = title;
		this.year = year;
	}

	@Override
	public String toString() {
		return type+":"+title+":"+year;
	}
}

public class WorksValue {
	private LinkedList<Works> list = new LinkedList<Works>();
	
	public LinkedList<Works> getList(){
		return list;
	}
	
	public WorksValue add(Works w) {
		list.add(w);
		return this;
	}

	@Override
	public String toString() {
		String works="";
		for(Works w : list) {
			works += w.toString() + "\n";    //composerKey 클래스에서 오버라이딩했던 toString()
		}
		return works;
	}
	
}

객체를 다시 리턴하니까 연속적으로 메서드 부르기 가능

package ex05.collections.map15;

import java.util.HashMap;
import java.util.Scanner;

public class HashMapMain {
	
	public static void InitHashMap(HashMap<ComposerKey, WorksValue> hMap) {
		
		ComposerKey cKey0 = new ComposerKey().setName("베토벤").setNation("독일");
		Works works00 = new Works().setTitle("운명").setType("교향곡").setYear("1809");
		Works works01 = new Works().setTitle("영웅").setType("교향곡").setYear("1800");
		WorksValue wv0 = new WorksValue();
		wv0.add(works00).add(works01);

		ComposerKey cKey1 = new ComposerKey().setName("파헬벨").setNation("뉘른베르크");
		Works works10 = new Works().setTitle("캐논").setType("변주곡").setYear("1756");
		WorksValue wv1 = new WorksValue();
		wv1.add(works10);
		
		ComposerKey cKey2 = new ComposerKey().setName("베르디").setNation("이탈리아");
		Works works20 = new Works().setTitle("맥베스").setType("오페라").setYear("1865");
		Works works21 = new Works().setTitle("아이다").setType("오페라").setYear("1871");
		WorksValue wv2 = new WorksValue();
		wv2.add(works20).add(works21);
		
		hMap.put(cKey0, wv0);
		hMap.put(cKey1, wv1);
		hMap.put(cKey2, wv2);		
	}
	
	public static void InputComposer(HashMap<ComposerKey, WorksValue> hMap) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("작곡가 이름은 >> "); String name = sc.nextLine();
		System.out.print("나라/도시 이름은 >> "); String nation = sc.nextLine();
		
		ComposerKey cKey = new ComposerKey(name, nation);
		if(hMap.containsKey(cKey)) {
			System.out.println(cKey + "는 존재합니다");
			WorksValue wv = hMap.get(cKey);
			System.out.println(wv);      //worksValue클래스에서 toString()을 오버라이딩했으므로 값출력.
		}else {
			System.out.println(cKey + "는 존재하지 않습니다");
		}
		
		sc.close();		
	}
	public static void main(String[] args) {
		HashMap<ComposerKey, WorksValue> hMap = new HashMap<ComposerKey, WorksValue>();

		InitHashMap(hMap);
		InputComposer(hMap);
	}
}

0개의 댓글

관련 채용 정보