본문 바로가기
알고리즘/개념정리

해쉬 사용하기, 객체 만들기

by 새싹감자 2023. 2. 17.
package algo;

import java.util.HashMap;
import java.util.Map;

public class hashMap {
	static class person{
		int age;
		String name;
		
		public person(int age,String name){
			this.age=age;
			this.name=name;
		}
	}
public static void main(String[] args) {
	Map<String, Integer> hm = new HashMap<>();
	hm.put("둘리", 10);
	hm.put("또치", 20);
	hm.put("마이콜", 30);

	//value존재 확인
   System.out.println("10은 존재하는가? " + hm.containsValue(10));
   System.out.println("50은 존재하는가? " + hm.containsValue(50));
	  
	//key존재 확인
	boolean isExistsOne = hm.containsKey("둘리");
	System.out.println("isExistsOne: " + isExistsOne);
	
	for (String key: hm.keySet()){
		System.out.println("{" + key + " => " + hm.get(key) + "}");
	    }
	  

	
	
   Map<Integer, person> hm2 = new HashMap<>();
	hm2.put(1, new person(20,"a"));
	hm2.put(2, new person(20,"b"));
	hm2.put(3, new person(20,"c"));

	System.out.println(hm2.get(2).age);
	System.out.println(hm2.get(2).name);
	

}
}

'알고리즘 > 개념정리' 카테고리의 다른 글

형 변환, 소숫점 출력  (0) 2023.02.18
순열조합 코드정리  (0) 2023.02.17
서로소 집합 만들기  (1) 2022.09.20
순열, 조합  (0) 2022.09.12
자바 정렬하기  (0) 2022.09.12

댓글