자바의 정석 11장 (30일차) - HashMap

728x90

 HashMap

 Map인터페이스를 구현한 클래스, 데이터를 키, 그리고 값으로 저장.

Set과 동일하게 중복을 허용하지 않지만, 값(value)에 한해서 중복을 허용한다. 

 

만약 순서를 정렬하고 싶다면 LinkedHashMap클래스를 사용한다.

 

*TreeMap은 TreeSet과 동일한 기능이며 Map을 구현한 클래스라는 점에만 차이가 있다. 

TreeSet과 동일하게 데이터 삭제, 추가에 시간이 더 소요된다.


HashMap의 특징

해싱(hashing)기법으로 데이터를 저장하기 때문에 데이터의 검색이 빠르다

  • 해쉬(hash)함수를 사용하여 저장위치(index)를 지정하여 저장하고, 그 데이터를 불러올 때는 hash table에 접근하여 그 데이터를 가져온다.
  • 접근성, 즉 hash를 사용하여 데이터를 카테고리화 시키고, 그 카테고리안의 데이터만 검색하여 불러오게되므로 검색이 빠르다.
  • 배열(접근성) + LinkedList(데이터 검색 최적화, next()등 사용)

 

hashing을 사용하여 데이터를 불러오는 과정

  • key로 호출하여 해시코드(배열의 index)를 얻고 해당 배열을 접근
  • LinkedList를 통해 그 해시코드안의 데이터 중 검색한 데이터를 불러온다.
    • 따라서 같은 key에 대해 항상 같은 해시코드를 반환해야하므로 오버라이딩이 필요하다

HashMap 메서드

  • put()
  • putAll()
  • entrySet()
  • KeySet()
  • Values()
  • get()
  • getorDefault()
  • containKey()
  • containValue()

아래 코드 참조

		HashMap hmap = new HashMap();
		Map map = new HashMap();
		map.put("Map Instance", "test");
		
		hmap.put("BTC","메이저");
		hmap.put("XRP","메이저?");
		hmap.put("XRP","알트");
//		List나 Set과는 다르게 put을 사용한다
//		키 값이 겹치면 오류를 반환하는 것이 아니라 새로운 값으로 덮어씌운다.
//		{BTC=메이저, XRP=알트}
		
		hmap.putAll(map);
//		다른 맵의 객체를 넣을 때는 putAll
		
		System.out.println("전체 값만 출력: " + hmap.entrySet());
//		전체 값만 출력: [BTC=메이저, XRP=알트, Map Instance=test]
		System.out.println("키 값만 출력: " + hmap.keySet());
//		키 값만 출력: [BTC, XRP, Map Instance]
		System.out.println("value 값만 출력: " + hmap.values());
//		value 값만 출력: [메이저, 알트, test]
		System.out.println();

		System.out.println("Object.get함수를 사용해 BTC의 키 값만 출력: " + hmap.get("BTC"));
//		Object.get함수를 사용해 BTC의 키 값만 출력: 메이저
		System.out.println("키값만 대신 출력" + hmap.getOrDefault("AAA", "value가 아닌 키값 대신 출력"));
//		키값만 대신 출력value가 아닌 키값 대신 출력
		System.out.println(hmap.containsKey("XRP")); //true
		System.out.println(hmap.containsValue("NOPE")); //false

HashMap을 사용한 예제

1. 아이디와 비밀번호를 입력받고 일치하는 값이 없으면 일치하지 않다고 출력, 일치하면 일치함으로 출력

//		===================== ID / PW 확인 =====================
		Map IDPW = new HashMap();
		IDPW.put("aaa", "1234");
		IDPW.put("bbb", "4567");
		IDPW.put("ccc", "8910");
		int chance = 3;
		Scanner scanner = new Scanner(System.in);
		while(chance!=0) {
			System.out.println("아이디를 입력해주세요");
			String ID = scanner.nextLine().trim();
			System.out.println("비밀번호를 입력해주세요");
			String PW = scanner.nextLine().trim();
			if(IDPW.containsKey(ID) && IDPW.get(ID).equals(PW)) {
//				아이디가 현재 hashMap안에 key값으로 들어있고 입력받은 ID의 값이 현재 PW값과 일치(equals)하면
//				일치합니다 출력하고 break
				System.out.println("일치합니다");
				break;
			}
			else if(chance>1) {
				chance--;
				System.out.printf("%d번더 틀리시면 프로그램이 종료됩니다. \n", chance);
				continue;
			}
			else {
				System.out.println("기회 전부 소진");
				break;
			}
		}

 

 

2. 배열에 담겨있는 String 데이터의 개수를 확인 후 개수만큼 #으로 출력

 

출력문:

####+A
####+B
###+C
##+D
#+E

//		===================== 빈도 수 확인 =====================
		
		String[] data = {"A","A","A","B","B","B","A","C","C","B","C","D","D","E"};
		Map mp2 = new HashMap();
		
		for(int i = 0; i<data.length; i++) {
			if(mp2.containsKey(data[i])) {
				int value = (int)mp2.get(data[i]);
				mp2.put(data[i], value + 1);
			}
			else {
				mp2.put(data[i], 1);
			}
		}
		System.out.println(mp2);

		Set set2 = mp2.entrySet();
		Iterator it2 = set2.iterator();
		
		while(it2.hasNext()) {
			Map.Entry e = (Map.Entry)it2.next();
			for(int i = 0; i < (int)e.getValue(); i++) {
				System.out.print("#");
			}
			System.out.print("+" + e.getKey());
			System.out.println();
		}

 

 

2. 각 Map의 키, value값 확인 후 총점, 평균 최고점, 최저점 출력

 

출력문:

참가자 이름: 이백도 점수: 57
참가자 이름: 홍길동 점수: 94
참가자 이름: 이순신 점수: 88
참가자 이름: 감강찬 점수: 83
참가자 이름: 장영실 점수: 100
참가자 명단: [이백도, 홍길동, 이순신, 감강찬, 장영실]
총점: 422
평균: 84.4
최고점: 100
최저점: 57
{A=4, B=4, C=3, D=2, E=1}

//		===================== 총점 / 평균 확인 =====================
		Map mp = new HashMap();
		int total = 0;
		double ave = 0.0;
		Scanner scanner2 = new Scanner(System.in);
		mp.put("홍길동", 94);
		mp.put("이순신", 88);
		mp.put("장영실", 100);
		mp.put("감강찬", 83);
		mp.put("이백도", 57);
		
		Set set = mp.entrySet();
		Iterator it = set.iterator();
		while(it.hasNext()) {
			Map.Entry e = (Map.Entry)it.next();
			System.out.println("참가자 이름: " + e.getKey() + " 점수: " + e.getValue());
		}
		
		System.out.println("참가자 명단: " + mp.keySet());
		
		for(Object list : mp.values()) {
			int tmp = (Integer)list;
			total+=tmp;
		}
		System.out.println("총점: " + total);
		
		ave = (double)total/mp.size();
		System.out.println("평균: " + ave);
		System.out.println("최고점: " + Collections.max(mp.values()));
		System.out.println("최저점: " + Collections.min(mp.values()));

 

728x90