자바 API 6 [HashMap, HashSet, Iterator]

 

 Map계열의 자료구조는 List계열과 달리 인덱스가 없고, 키와 값만 있다.
그리고 당연한 얘기지만, 키는 유니크 해야 한다. 

- HashMap

키와 값으로 이루어져 있으며, 키는 유니크 해야하고, 값은 중복되어도 상관없다.
 key값으로 컨트롤 되는 컬렉션 클래스이기 때문이다.


HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

hashMap.put(0, "str0");
hashMap.put(1, "str1");
hashMap.put(2, "str2");
hashMap.put(3, "str3");

System.out.println(hashMap.toString());

String str = hashMap.get(2);
System.out.println(str);

hashMap.remove(2);
System.out.println(hashMap.toString());

hashMap.clear();
System.out.println(hashMap.toString());

예제 코드를 보면
첫째 줄에서 hashMap변수를 선언 및 초기화할 때, key와 value의 타입을 지정해주었다.
HashMap다음 꺽새('<', '>')안에 첫번째로 오는 것이 key가 될 값의 타입을 지정해 준 것이며,
두번째로 오는 것이 value의 타입을 지정해 준것이다.

따라서 세번째 줄을 분석하자면, 
hashMap에 "str0"문자열을 저장하면서 그 값의 '키'값으로 숫자 0을 할당시키고 있다. 

해당 "str0"문자열을 hashMap변수로부터 가져오려면 아래와 같이 키값을 이용해 가져오면된다.
hashMap.get(0);    // * 여기서 0은 배열이나 list계열 컬렉션에서 쓰이는 인덱스(index)가 아니다. 


HashMap에서 기본적으로 사용되는 메소드들은 다음과 같다.
hashMap.put(key, value);
hashMap.get(key);
hashMap.remove(key);
hashMap.clear();

각 메소드에 대한 결과는 위 예제코드에 대한 결과를 통해 확인할 수 있다.

{0=str0, 1=str1, 2= str2, 3=str3}
str2
{0=str0, 1=str1, 3=str3}
{}

 

 


- Iterator

Iterator는 반복자이다.  데이터를 반복적으로 검색하는데 아주 유용한 인터페이스 이며, 
모든 자료구조형은 iterator()메소드를 지원하고 있다.

예제 코드를 보면 쉽게 이해할 수 있다.


HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

hashMap.put(0, "str0");
hashMap.put(1, "str1");
hashMap.put(2, "str2");
hashMap.put(3, "str3");
System.out.println(hashMap.toString());

// Iterator 시작부
Iterator<Integer> iterator = hashMap.keySet().iterator();
while(iterator.hasNext()) {
  String str = hashMap.get(iterator.next());
  System.out.println(str);
}

Iterator안에 데이터는 Integer형이 들어가며 hashMap의 keySet()으로 부터 데이터를 얻어온다.
hashMap의 keySet()은 0,1,2,3

예제코드에서 10번째 줄은 hashMap의 keySet()을 반복자(iterator)로 지정하여 Iterator 객체를 생성및 선언하는 부분이다.

> Iterator의 메소드
1) hasNext() 메소드는 반복자의 다음 순번 값의 존재 여부를 반환한다.
    다음 순번 값이 없으면 false를 반환하므로 반복문은 종료된다.
2) next() 메소드는 다음 순번의 값을 반환한다. 

따라서 위의 예제코드를 실행하면 결과는 다음과 같다.

{0=str0, 1=str1, 2= str2, 3=str3}
str0
str1
str2
str3

 

 


- HashSet

Set계열 자료구조에서는 데이터의 순서는 없다. 지만 중복된 데이터는 허락하지 않는다.

 


HashSet<String> hashSet = new HashSet<String>();

hashSet.add("str0");
hashSet.add("str1");
hashSet.add("str2");
hashSet.add("str3");
hashSet.add("str2");
System.out.println(hashSet.toString());

hashSet.remove("str0");
System.out.println(hashSet.toString());

int i = hashSet.size();
System.out.println("사이즈 : "+ i);

결과는 아래와 같다.
{str3, str1, str2, str0}
{str3, str1, str2}
사이즈 : 3

순서가 없으므로 add() 메소드를 이용해 데이터를 넣는 것과 상관없이, 데이터가 뒤죽박죽 들어간다.

위의 예제에서는 HashSet이 기초자료형인 String타입으로 만들어져 똑같은 문자열"str2"을 두번 add할 때, 자동으로 걸려졌으나 객체자료형으로 HashSet을 사용할 때는 주의가 필요하다.

 

 

 

이미지를 보면, 이름이 이순신이고 학년이 6인 학생(객체자료형)을 HashSet에서 remove메소드를 이용하려 제거 하려고 하였으나 지워지지 않은 것을 확인할 수 있다.

이는 처음에 hashSet변수에 add메소드를 이용하여 만들어진 "이순신"학생과
remove하기 위해 새로 new 생성자를 이용하여 만들어진 "이순신"학생은 서로 다른 주소값을 가지고 있기 때문이다.

때문에 다음과 같은 방법을 이용한다.

 

 

 

우측의 빨간 네모상자 안과 같이 equals와 hashCode를 내가 만든 객체에 오버라이드 해준다.
그러고 나면 remove메소드 호출 시 정상적으로 제거되는 것을 확인할 수 있다.

equals에서는 새로 들어온 object의 toString값과 현재 가지고 있는 객체의 toString값을 비교하여 return하고, 
hashCode는 해당 객체의 hashCode를 정수형으로 뽑아주는 역할을 한다.
hashCode와 equals를 재정의 해줌으로써 두 객체가 같다고 보도록 만든것이다. 

 

 

'Java' 카테고리의 다른 글

자바 예외처리 [Exception]  (0) 2020.01.22
자바 API 7 [Random과 SecureRandom의 차이]  (0) 2020.01.21
자바 API 5 [InputStream,OutputStream]  (0) 2020.01.21
자바 API 4 [StringTokenizer]  (0) 2020.01.20
자바 API 3 [Timer]  (0) 2020.01.20

+ Recent posts