[JAVA]22일차(컬렉션 프레임워크(Collection Framework)/Map/Hashtable/HashMap)
8월13일(금)
Test160~Test168
◼ Map
// Map 은 다른 자료구조보다 사용빈도가 높다!
Map -> Hashtable, HashMap
- 두개는 기능은 비슷. 해시테이블은 동기화기능이 있고 해시맵은 동기화 기능이 없다.
- java.util.Map 인터페이스는
키(key)를 값(value)에 매핑(mapping)하며,
동일한 키를 등록할 수 없고, 유일해야 하며(고유해야 하며)
각 키는 한 개의 값만을 매핑해야 한다.
즉, 하나의 키 값에 대응하는 하나의 값을 갖는 자료구조이다.
▪ Hashtable<K, V> 클래스
-
해시테이블 구조를 객체 모델링한 클래스로
검색이 용이하기 때문에 사용 빈도가 높은 편에 속한다. -
해시테이블이란 키(key)와 그에 상응하는 데이터(value)로
구분된 데이터 구조이다. -
데이터를 저장하고, 검색하기 위해서 키(key)로 접근하며
이 구조에서는 키 값을 부여하면 해당 데이터가 변환된다. -
또한, Hashtable 클래스는 key 또는 value 의 값으로 null 을 허용하지 않는다. ★★★
▪ Hashtable 코드📝
import java.util.Hashtable;
public class Test166
{
private static final String[] names
= {"서효진","김효진","최효진","손효진","최효진2","이효진" };
private static final String[] tels
={"010-0000-0000","010-1111-1111","010-2222-2222"
, "010-3333-3333", "010-4444-4444", "010-5555-5555"};
public static void main(String[] args)
{
// Hashtable 자료구조 인스턴스 생성
Hashtable<String, String> ht = new Hashtable<String, String>(); // key 랑 value 값 제네릭으로
// ht 라고 명명한 Hashtable 자료구조에
// 배열(names, tels)에 담겨있는 데이터를 요소로 담아내기
// -> put(k, v) //해시테이블에 자료를 담는 가장 기본적인 메소드
for (int i=0; i<names.length; i++)
{
ht.put(names[i],tels[i]);
}
// ht 라는 Hashtable 의 값을... key 를 이용하여 검색
// -> get(k)
String findName1 = "최효진";
String str = ht.get(findName1);
if (str != null)
{
System.out.println(findName1 + "전화번호 : " + str);
}
System.out.println();
//--==>> 최효진전화번호 : 010-3288-6988
// ht 라는 Hashtable 자료구조에... key 가 존재하는지의 여부 확인
// -> containsKey()
String findName2 = "윤효진";
if (ht.containsKey(findName2))
{
System.out.println(findName2 + "데이터가 존재합니다.");
}
else
System.out.println(findName2 + "데이터가 존재하지 않습니다.");
System.out.println();
//--==>> 윤효진데이터가 존재하지 않습니다.
String findName3 = "이효진";
if (ht.containsKey(findName2))
{
System.out.println(findName3 + "데이터가 존재합니다.");
}
else
System.out.println(findName3 + "데이터가 존재하지 않습니다.");
System.out.println();
//--==>> 이효진데이터가 존재하지 않습니다.
// ht 라는 Hashtable 자료구조에... value 가 존재하는지의 여부 확인
// -> contains()
String findTel1 = "010-0000-0000";
if (ht.contains(findTel1))
System.out.println(findTel1 + "데이터가 존재합니다.");
else
System.out.println(findTel1 + "데이터가 존재하지 않습니다.");
System.out.println();
//--==>> 010-0000-0000데이터가 존재합니다.
// ht 라는 Hashtable 자료구조에서... 『김효진』데이터 삭제
// -> remove()
ht.remove("김효진");
//-- 『remove』 메소드 인자값으로 key 를 넘겨받지만
// 이 때, key 만 삭제되는 것이 아니다.
// 해당 key 와 연결되어(매핑되어) 있는 value 도 함께 remove() 된다.
//삭제(remove()) 이후 key 가 존재하는지 확인
if(ht.containsKey("김효진"))
System.out.println("김효진이 존재합니다.");
else
System.out.println("김효진 어디갔어!?!?");
System.out.println();
//--==>> 김효진 어디갔어!?!?
// 삭제(remove()) 이후 value 가 존재하는지 확인
if(ht.contains("010-1111-1111"))
System.out.println("010-1111-1111데이터가 존재합니다.");
else
System.out.println("010-1111-1111 데이터가 존재하지 않습니다.");
System.out.println();
//--==>> 010-1111-1111 데이터가 존재하지 않습니다.
// 마치 가비지 컬렉션과 같은 구조이다!!
// null 관련 처리 관찰
// null은 히든 벨류든 해시테이블에 적재할 수없다!
//ht.put(null, null); //-- key 와 value 모두 null → 런타임 에러(NullPointerException)
//ht.put("장효진",null); //-- value 가 null → 런타임 에러(NullPointerException)
//ht.put(null,"010-6666-666"); //-- key 가 null → 런타임 에러(NullPointerException)
// 중복된 key 입력
ht.put("손효진","010-1234-1234");
System.out.println(ht.get("손효진"));
System.out.println();
//--==>> 010-1234-1234
//-- 기존 "010-3333-3333" 에서 "010-1234-1234"로 변경되었음을 확인
// (덮어쓰기 개념)
// 중복된 value 입력
ht.put("권효진","010-2222-2222");
System.out.println(ht.get("최효진"));
System.out.println(ht.get("권효진"));
System.out.println();
//--==>> 010-2222-2222
// 010-2222-2222
// ※ value 는 중복된 값이 입력되어도 기본 데이터에 아무런 영향을 미치지 않음.
}
}
▪ HashMap<K, V> 클래스
-
Hashtable 클래스와 마찬가지로 Map 인터페이스에서 상속받은
HashMap 클래스의 기능은 Hashtable 과 동일하지만
Synchronozation 이 없기 때문에 동시성 문제가 없다면
(즉, 멀티 스레드 프로그램이 아닌 경우라면)
HashMap 을 사용하는 것이 성능을 향상시킬 수 있다.
(Hashtable보다 성능이 좋다!) -
또한, HashMap 은 Hashtable 클래스와 달리 null을 허용한다. ★★★
※ 비어있는 값도 별도로 체크해서 처리하려면 HashMap을 써야함!
▪ HashMap 코드📝
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Test167
{
public static void main(String[] args) throws IOException
{
// HashMap 자료구조 인스턴스 생성
//HashMap<String, String> map = new HashMap<String, String>(); //리스트 이외에는 인터페이스 자료구조로 생성해주는거 없읍!(비교)
Map<String,String> map = new HashMap<String,String>();
// map 이라는 해시맵 자료구조에 요소 추가
// -> put()
map.put("드라마","빈센조");
map.put("영화","그래비티");
map.put("만화","도라에몽");
// 더미 확인
System.out.println(map);
//--==>> {드라마=빈센조, 영화=그래비티, 만화=도라에몽}
// null 입력 시도
map.put(null, null);
map.put("소설", null);
map.put(null,"생각하는사람");
//--==>> 에러 안남!
//-- 위의 데이터 입력 유형에 따라 모든 종류의 데이터 입력이 가능하지만
// 최초 null 을 key로 매핑하는 null 을 덮어쓰는 상황이 발생하게 된다.
// 즉, HashMap 은 null을 하나의 고유한 key 값으로 간주한다.(인식한다.)
// 더미 확인
//System.out.println(map);
//--==>> {null=생각하는사람, 소설=null, 드라마=빈센조, 영화=그래비티, 만화=도라에몽} // nulln, null 덮어쓰기 발생
//map.put(null,"생각하는사람"); 이거 주석처리하면!
//--==>> {null=null, 소설=null, 드라마=빈센조, 영화=그래비티, 만화=도라에몽}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("카테고리, 타이틀 입력(컴마 구분) : "); //"그림,모나리자"
String[] temp;
for (String str; ((str=br.readLine()) != null); )
{
temp = str.split(","); // {"그림", "모나리자""}
if(temp.length<2)
{
System.out.print("카테고리, 타이틀 입력(컴마 구분) : ");
continue;
}
map.put(temp[0].trim(), temp[1].trim());
System.out.println(map);
}
}
}
// 일괄 작업을 끝내시겠습니까 (Y/N)? -> ctrl + c
// 다집지 못하고 넘어온 컬렉션에 주요 클래스 주요 메소크가 많음!! 따로 공부하자!
Author And Source
이 문제에 관하여([JAVA]22일차(컬렉션 프레임워크(Collection Framework)/Map/Hashtable/HashMap)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@hyojin_j/JAVA22일차컬렉션-프레임워크Collection-FrameworkMap...ing저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)