본문 바로가기
개발/Java

Map - LinkedHashMap 사용하는 방법

by Devsong26 2017. 11. 5.

- LinkedHashMap이란?

Map 인터페이스를 구현한 클래스이며 동시에 Linked LIst이면서 Hash table입니다. 

 

 

- 특징

(1) 입력했던 순서대로 Entry가 LinkedHashMap에 mapping됩니다.

(2) LinkedHashMap은 double-linked List로 모든 Entry를 유지합니다. 

(3) key, value에 null 입력이 가능합니다.

(4) get, put, remove, containsKey 메소드를 호출할 때 O(1)의 시간복잡도를 갖습니다.  

 

 

- 생성자

//첫번째 생성자
LinkedHashMap<String, Integer> map1 = new LinkedHashMap<String, Integer>();

//두번째 생성자
LinkedHashMap<String, Integer> map2 = new LinkedHashMap<String, Integer>(10);

//세번째 생성자
LinkedHashMap<String, Integer> map3 = new LinkedHashMap<String, Integer>(10, 0.75f);

//네번째 생성자
LinkedHashMap<String, Integer> map4 = new LinkedHashMap<String, Integer>(10, 0.75f, true);

//다섯번째 생성자
LinkedHashMap<String, Integer> map5 = new LinkedHashMap<String, Integer>(map1);

 

첫번째 생성자는 기본생성자로써 initial capacity(16), load factor(0.75)값을 가진 객체를 생성합니다. 

두번째 생성자는 매개변수로 입력된 initial capacity값과 load factor(0.75)값을 가진 객체를 생성합니다.

세번째 생성자는 매개변수로 입력한 initial capacity, load factor값을 가진 객체를 생성합니다. 

네번째 생성자는 initial capacity, load factor, accessOrder를 매개변수로 입력하여 객체를 생성합니다. 

(아래에서 추가로 다루겠습니다.)

다섯번째 생성자는 매개변수로 Map객체를 입력하여 객체를 생성합니다. 생성된 객체는 Map객체의 mapping을 갖습니다. 

 

네번째 생성자에 대해 알아보겠습니다.

(1) accessOrder : true일 경우

 

LinkedHashMap<String, Integer> map1 = new LinkedHashMap<String, Integer>(16, 0.75f, true);

map1.put("first", 2);
map1.put("second", 3);
map1.put("third", 4);
map1.put("fourth", 5);

map1.get("first");
map1.get("third");

map1.forEach((key, value) -> {
	System.out.println("Key:" + key + ", Value:" + value);
});

 

 결 과
 Key:second, Value:3
 Key:fourth, Value:5
 Key:first, Value:2
 Key:third, Value:4

 

(2) accessOrder : false일 경우

 

LinkedHashMap<String, Integer> map2 =LinkHashMap<String, Integer>(16, 0.75f, false);

map2.put("first", 2);
map2.put("second", 3);
map2.put("third", 4);
map2.put("fourth", 5);

map2.get("first");
map2.get("third");

map1.forEach((key, value) -> {
	System.out.println("Key:" + key + ", Value:" + value);
});

 

  결 과
 Key:first, Value:2
 Key:second, Value:3
 Key:third, Value:4
 Key:fourth, Value:5

 

 

accessOrder는 Entry에 access하는 mode를 나타냅니다. 

true일 경우 입력된 순서 중에 access빈도 낮은 것들부터 접근합니다. 

false일 경우 입력된 순서로 Entry에 접근합니다. 

map1의 경우 accessOrder를 true로 지정하고 생성하였습니다. 

get메소드에 first, third를 access하고 나면 first, third는 access빈도가 1이고, second, fourth의 경우 access빈도가 0입니다. 

그래서 forEach()메소드를 통해서 결과를 출력하면 빈도수가 낮은 순부터 출력되는 것을 볼 수 있습니다. 

 

* forEach() 옆에 -> 가 있는데 이것은 람다식 입니다. (람다식은 아직 공부를 하지 않았습니다.)

 

 

- 메소드

모든 메소드를 설명하지 않습니다. 

 

(1) clear()    반환형: void

LinkedHashMap객체의 모든 mapping을 삭제합니다. 

 

(2) containsValue(Object value)    반환형: boolean

매개변수로 입력된 value값이 LinkedHashMap 객체에 존재한다면 true, 없으면 false를 반환합니다. 

 

(3) entrySet()    반환형: Set<Map.Entry<K,V>>

http://developer-syubrofo.tistory.com/7 에서 확인하실 수 있습니다.

 

(4)  get(Object key)    반환형: K

LinkedHashMap 객체에 key에 해당하는 value가 있다면 그 값을 반환하고, 없으면 null을 반환합니다.

 

(5) getOrDefault(Object key, V defaultValue)    반환형: V

LinkedHashMap 객체에 key에 해당하는 value값이 있다면 그값을 반환하고, 없으면 매개변수로 입력된 defaultValue값을 반환합니다. 

 

(6) keySet()    반환형: Set<K>

LinkedHashMap 객체에 모든 key값을 가진 Set객체를 반환합니다. 

 

이상으로 포스팅을 마칩니다. 

 

 

 

* forEach()는 람다식 공부 후 업데이트 하겠습니다.

 


더 많은 내용을 보시려면 아래를 참고하세요.


블로그의 다른 글

 

maven없이 스프링(Spring) 프로젝트 만들기 (1) - 프로젝트 생성하기

maven 외부 저장소에서 라이브러리를 프로젝트에 포함시킬 때 간혹 문제가 발생하는 경우가 있어서 maven이 없는 스프링 프로젝트를 만들어 보겠습니다. - Dynamic Web Project 생성하기 프로젝트 이름

developer-syubrofo.tistory.com

 

maven없이 스프링(Spring) 프로젝트 만들기 (2) - 웹 앱 실행하기

1부에서 작업한 코드로는 웹 프로젝트를 실행할 수 없습니다. 추가 작업을 하겠습니다. Java Resources -> src 우클릭 합니다. Name에 "spring.home.controller"를 입력 후 "Finish"를 누릅니다. 생성된 패키..

developer-syubrofo.tistory.com

 

maven없이 스프링(Spring) 프로젝트 만들기 (3) - 디비(DB, Database) 연동하기 #1

스프링 디비 연동 포스팅은 3파트로 나뉩니다. (1) DB 드라이버, ibatis, mybatis.jar 다운받기 및 셋팅 (2) DTO, DAO, mybatis 작업하기 (3) view 페이지 작업, Controller 작업하기 지금 보실 내용은 (1) DB..

developer-syubrofo.tistory.com

 

maven없이 스프링(Spring) 프로젝트 만들기 (3) - 디비(DB, Database) 연동하기 #2

스프링 디비 연동 포스팅은 3파트로 나뉩니다. (1) DB 드라이버, ibatis, mybatis.jar 다운받기 및 셋팅 (2) DTO, DAO, mybatis 작업하기 (3) view 페이지 작업, Controller 작업하기 지금 보실 내용은 (2) DTO, D..

developer-syubrofo.tistory.com

 

maven없이 스프링(Spring) 프로젝트 만들기 (3) - 디비(DB, Database) 연동하기 #3

스프링 디비 연동 포스팅은 3파트로 나뉩니다. (1) DB 드라이버, ibatis, mybatis.jar 다운받기 및 셋팅 (2) DTO, DAO, mybatis 작업하기 (3) view 페이지 작업, Controller 작업하기 지금 보실 내용은 (3) view..

developer-syubrofo.tistory.com