11/29 Map 인터페이스 🖼️

리무 rimu ·2023년 1월 5일
0

Java

목록 보기
19/35

Map 인터페이스

Hashtable / HashMap
식별자에 의미를 부여함, 값을 저장 할때
( key (식별자) / value(값)) 로 함
0,1,2이 아니라 내가 무엇을 찾고자하는지 직관적으로 알수 있게 함
예를 들면 전화번호부에 이름 값을 저장 할때 등 / 공간차지 多
출력할 부분 알고 꺼낼 수 있음
지명을 이용해서 위치를 알 수 있음~

import java.util.*;
class MapEx1 {
	public static void main(String[] args) {
		Hashtable<String, String> table = new Hashtable<String, String>();

		table.put("아이유", "010-1004-5858"); // 1개
		table.put("윤아", "010-1004-1004");
		table.put("김유정", "010-5858-5858");
		table.put("김혜수", "010-5858-1004");

		System.out.println(table.size());

		String tel = table.get("김유정");
		System.out.println(tel);
	}
}

기존에 들어간 정보가 있으면 그 정보는 사라져버림
같은 식별자(이름/key)가 있는지 없는지 확인해야 함, 아니면 기존에 데이터가 사라짐!

Iterator<E> Interface
자바의 컬렉션 프레임워크는 컬렉션에 저장된 요소를 읽어오는 방법을 Iterator 인터페이스로 표준화하고 있음

Collection 인터페이스에서는 Iterator 인터페이스를 구현한 클래스의 인스턴스를 반환하는 iterator() 메소드를 정의하여 각 요소에 접근하도록 하고 있습니다.
따라서 Collection 인터페이스를 상속받는 List와 Set 인터페이스에서도 iterator() 메소드를 사용할 수 있습니다.

import java.util.*;
class MapEx2 {
	public static void main(String[] args) {
		Hashtable(String, String> table = new Hashtable<String, String>();

		table.put("아이유", "010-1004-5858"); 
		table.put("윤아", "010-1004-1004");
		table.put("김유정", "010-5858-5858");
		table.put("김혜수", "010-5858-1004");

		table.remove("윤아");

		// key만 다 가져온다
		set<String> keys = table.keySet();
		for(String key : keys){
			String value = table.get(key);
			System.out.println(key + " : " + value);
		}

		/* 
		//Iterator : 반복자 
		Iterator<String> itr = keys.iterator();
		while(itr.hasNext()) {
			String key = itr.next();
			String value = table.get(key);
			System.out.println(key + " : " + value);
			*/

			//value만 다 가져온다.
			Collection<String> valuse = table.values();
			for(String value : values) {
				System.out.println(value);
			}
		}	
	}

모듈러연산자 %

해싱 Hashing

빨리 찾기 위한 방법 중 하나
해싱함수: 내가 찾고자하는 값을 잡아넣고 변환하는 것

// 복수 데이터 쓸 때 편하도록

  • hashCode():int <= 메서드
import java.util.*;

class OtherKey {
	public String word;
	public OtherKey(String word){
		this.word = word;
	}

	/*
		두 객체 a,b가 있을 때,
		만약 a.equals(b) == true 라면, 반드시 a.hashCode() == b.hashCode()가
		성립해야한다. (hashcode는 같은 그룹에 속한다고 생각) 하지만,
		a.hashCode() == b.hashCode()라고 해도, a.equals(b)가 true를 보장하지 않는다
	*/

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

	@Override
	public boolean equals(Object other){
		if(other == null || !(other instanceof OtherKey)) {
			return false;
		}
	// 해쉬코드 잡아넣고 하면 연산을 덜 하게 됨/ hashCode 오버라이드 했을때만 사용할 것!
		if(hashCode() != other.hashCode()){
			return false; 
		}
		OtherKey temp = (OtherKey)other;
		return word.equals(temp.word);
	}
}

class MapEx5 {
	public static void main(String[] args) {
		Hashtable<OtherKey, String> table = new Hashtable<OtherKey, String>();

		table.put(new OtherKey("banana"),"A");
		table.put(new OtherKey("orange"),"C");
		table.put(new OtherKey("kiwi"),"F");
		table.put(new OtherKey("mango"),"D");
		table.put(new OtherKey("apple"),"E");
		table.put(new OtherKey("tomato"),"H");

		System.out.println(table.get(new OtherKey("mango")));
	}
}
profile
JAVA / SQL / Spring 을 공부하고 있습니다 🐥

0개의 댓글