๐Ÿ’ป

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ(List, Set, Map) ๋ณธ๋ฌธ

KITRI/JAVA

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ(List, Set, Map)

๋˜ํšจ๋‹ˆ 2020. 5. 14. 11:31

์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ

 

: ์ปฌ๋ ‰์…˜์€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ์ฒด๋“ค์„ ํšจ์œจ์ ์œผ๋กœ ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋„๋ก java.util ํŒจํ‚ค์ง€์— ๊ด€๋ จ๋œ ์ธํ„ฐํŽ˜์ด์Šค์™€ ํด๋ž˜์Šค๋ฅผ ํฌํ•จ์‹œ์ผœ ๋†“์•˜๋‹ค. ์ด๋ฅผ ์ด์นญํ•ด์„œ ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

Q. ์ปฌ๋ ‰์…˜์ด ๋ญ๋‹ˆ?
A. ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค์™€ ํด๋ž˜์Šค๋ฅผ ํฌํ•จ์‹œ์ผœ ๋†“์€ ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ž๋ฐ” ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์ฃผ์š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ๋Š” List, Set, Map ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

Q. ํ”„๋ ˆ์ž„์›Œํฌ๋ž€?

A. ์‚ฌ์šฉ๋ฐฉ๋ฒ•์„ ๋ฏธ๋ฆฌ ์ •ํ•ด ๋†“์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

 


 *  1) List: ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์— ๋“ค์–ด๊ฐ€๋Š” ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋‹ค.
              ArrayList, Vector, LinkedList, Stack, Queue ๋“ฑ๋“ฑ
 *  2) Set: ์ €์žฅ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด๋ฅผ ์ค‘๋ณตํ•ด์„œ ์ €์žฅํ•  ์ˆ˜ ์—†๊ณ , ํ•˜๋‚˜์˜ null๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. 
              HashSet, TreeSet ๋“ฑ๋“ฑ
 *  3) Map: ์ˆœ์„œ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†์ง€๋งŒ ํ‚ค ๊ฐ’์ด ์žˆ๋‹ค.
               HashMap, HashTable, TreeMap, Properties ๋“ฑ๋“ฑ

 

์ธํ„ฐํŽ˜์ด์Šค ๋ถ„๋ฅ˜ ํŠน์ง• ๊ตฌํ˜„ ํด๋ž˜์Šค
Collection List - ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์ €์žฅ
- ์ค‘๋ณต ์ €์žฅ ๊ฐ€๋Šฅ
ArrayList, Vector, LinkedList
Set - ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜์ง€ ์•Š๊ณ  ์ €์žฅ
- ์ค‘๋ณต ์ €์žฅ ์•ˆ ๋จ
HashSet, TreeSet
Map - ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ์ €์žฅ
- ํ‚ค๋Š” ์ค‘๋ณต ์ €์žฅ ์•ˆ ๋จ
HashMap, Hashtable, TreeMap, Properties

 

 

 

 

 

List ์ปฌ๋ ‰์…˜

 

: List ์ปฌ๋ ‰์…˜์€ ๊ฐ์ฒด๋ฅผ ์ผ๋ ฌ๋กœ ๋Š˜์–ด๋†“์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ฐ์ฒด๋ฅผ ์ธ๋ฑ์Šค๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ธ๋ฑ์Šค๊ฐ€ ๋ถ€์—ฌ๋˜๊ณ  ์ธ๋ฑ์Šค๋กœ ๊ฐ์ฒด๋ฅผ ๊ฒ€์ƒ‰, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. List์ปฌ๋ ‰์…˜์€ ๊ฐ์ฒด ์ž์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ์ฒด์˜ ๋ฒˆ์ง€๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. 

 

heap     stack  
new AA Ox100   a Ox100
new AA Ox200   b Ox200
new AA Ox300   c Ox300
         
new ArrayList Ox400   array Ox400
[0] (=0๋ฒˆ์ง€) Ox100 Ox200(๋‹ค์Œ ๋ฒˆ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค=>์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ)    
[1] (=1๋ฒˆ์ง€) Ox200      
[2] (=2๋ฒˆ์ง€) Ox300      

 

List ์ปฌ๋ ‰์…˜์—๋Š” ArrayList, Vector, LinkedList ๋“ฑ์ด ์žˆ๋Š”๋ฐ, ๋‹ค์Œ์€ List ์ปฌ๋ ‰์…˜์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ List ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ฉ”์†Œ๋“œ๋“ค์ด๋‹ค. ์ธ๋ฑ์Šค๋กœ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋ฅผ ๋งค๊ฐœ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ๋งŽ๋‹ค.

 

๋ฉ”์†Œ๋“œ ๊ธฐ๋Šฅ ์„ค๋ช…
boolean add(E e) ๊ฐ์ฒด ์ถ”๊ฐ€ ์ฃผ์–ด์ง„ ๊ฐ์ฒด๋ฅผ ๋งจ ๋์— ์ถ”๊ฐ€
void add(int index, E element) ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์— ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€
E set(int index, E element) ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ์ฃผ์–ด์ง„ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟˆ
boolean contains(Object o) ๊ฐ์ฒด ๊ฒ€์ƒ‰ ์ฃผ์–ด์ง„ ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€
E get(int index) ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด
boolean isEmpty() ์ปฌ๋ ‰์…˜์ด ๋น„์–ด ์žˆ๋Š”์ง€ ์กฐ์‚ฌ
int size() ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ „์ฒด ๊ฐ์ฒด ์ˆ˜๋ฅผ ๋ฆฌํ„ด
void clear() ๊ฐ์ฒด ์‚ญ์ œ ์ €์žฅ๋œ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์‚ญ์ œ
E remove(int index) ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ์‚ญ์ œ
boolean remove(Object o) ์ฃผ์–ด์ง„ ๊ฐ์ฒด๋ฅผ ์‚ญ์ œ

 

์–‘์ชฝ์œผ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ -> C๋กœ ๋”ฐ์ง€๋ฉด ๋‹ค์ค‘ํฌ์ธํ„ฐ

์™ผ์ชฝ,์˜ค๋ฅธ์ชฝ ์ ‘๊ทผ -> ๋‹ค์ค‘๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ

๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ, ๋‹ค์ค‘๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ

 

 

 

 

ArrayList

 

: List ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„ ํด๋ž˜์Šค๋กœ, ์ผ๋ฐ˜๋ฐฐ์—ด๊ณผ ArrayList๋Š” ์ธ๋ฑ์Šค๋กœ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ์ ์—์„œ๋Š” ์œ ์‚ฌํ•˜์ง€๋งŒ, ํฐ ์ฐจ์ด์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋ฐฐ์—ด์€ ์ƒ์„ฑํ•  ๋•Œ ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜๊ณ  ์‚ฌ์šฉ ์ค‘์— ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์ง€๋งŒ,  ArrayList๋Š” ์ €์žฅ์šฉ๋Ÿ‰(capacity)์„ ์ดˆ๊ณผํ•œ ๊ฐ์ฒด๋“ค์ด ๋“ค์–ด์˜ค๋ฉด ์ž๋™์ ์œผ๋กœ ์ €์žฅ ์šฉ๋Ÿ‰์ด ๋Š˜์–ด๋‚œ๋‹ค. 

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/ArrayList.html

 

ArrayList (Java SE 14 & JDK 14)

Type Parameters: E - the type of elements in this list All Implemented Interfaces: Serializable, Cloneable, Iterable , Collection , List , RandomAccess Direct Known Subclasses: AttributeList, RoleList, RoleUnresolvedList public class ArrayList extends Abst

docs.oracle.com

 

 

 

 

ArrayList ๋Š” SELECTํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋Š” (1)๊ธฐ๋ณธ์ž๋ฃŒํ˜•(0:์„ฑ๊ณต 1:์‹คํŒจ), (2)ํ•œ ํ–‰์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ, (3)2๊ฐœ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ(๋ ˆ์ฝ”๋“œ ๋‹จ์œ„)๋ฅผ ๊ฐ€์ ธ ์˜ค๋Š” ๊ฒƒ. ์˜ ์„ธ ๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค. 

 

ArrayList๋Š” ๊ฐ์ฒด๋ฅผ ๋‹ด์•„์ค€๋‹ค. ๋ ˆ์ฝ”๋“œ ๋‹จ์œ„๋กœ ์ €์žฅํ•˜๋ ค๋ฉด ArrayList๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฐฐ์—ด์˜ ๋‹จ์  ์ถ”๊ฐ€ ์‚ญ์ œ ์‚ฝ์ž…์ด ์•ˆ๋˜์„œ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•๋งŒ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

Vector

: ArrayList์™€ ๋™์ผํ•œ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ๋‹ค๋ฅธ ์ ์€ Vector๋Š” ๋™๊ธฐํ™”๋œ(synchronized) ๋ฉ”์†Œ๋“œ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์ด ๋ฉ”์†Œ๋“œ๋“ค์„ ์‹คํ–‰ํ•  ์ˆ˜ ์—†๊ณ , ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰์„ ์™„๋ฃŒํ•ด์•ผ๋งŒ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์•ˆ์ „ํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์„ ์Šค๋ ๋“œ๊ฐ€ ์•ˆ์ „(Thread Safe)ํ•˜๋‹ค๋ผ๊ณ  ๋งํ•œ๋‹ค.

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Vector.html

 

Vector (Java SE 14 & JDK 14)

Type Parameters: E - Type of component elements All Implemented Interfaces: Serializable, Cloneable, Iterable , Collection , List , RandomAccess Direct Known Subclasses: Stack public class Vector extends AbstractList implements List , RandomAccess, Cloneab

docs.oracle.com

Q. Vector์™€ ArrayList์˜ ์ฐจ์ด?

A. 

 

 

 

 

Stack

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Stack.html

 

Stack (Java SE 14 & JDK 14)

All Implemented Interfaces: Serializable, Cloneable, Iterable , Collection , List , RandomAccess public class Stack extends Vector The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that all

docs.oracle.com

 

 

 

 

Queue

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Queue.html

 

Queue (Java SE 14 & JDK 14)

Type Parameters: E - the type of elements held in this queue All Superinterfaces: Collection , Iterable All Known Subinterfaces: BlockingDeque , BlockingQueue , Deque , TransferQueue All Known Implementing Classes: AbstractQueue, ArrayBlockingQueue, ArrayD

docs.oracle.com

 

 

LinkedList

: List๊ตฌํ˜„ ํด๋ž˜์Šค์ด๋ฏ€๋กœ ArrayList์™€ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๋˜‘๊ฐ™์ง€๋งŒ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅด๋‹ค. ArrayList๋Š” ๋‚ด๋ถ€ ๋ฐฐ์—ด์— ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•ด์„œ ์ธ๋ฑ์Šค๋กœ ๊ด€๋ฆฌํ•˜์ง€๋งŒ, LinkedList๋Š” ์ธ์ ‘ ์ฐธ์กฐ๋ฅผ ๋งํฌํ•ด์„œ ์ฒด์ธ์ฒ˜๋Ÿผ ๊ด€๋ฆฌํ•œ๋‹ค. ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ์ธ๋ฑ์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ์•ž๋’ค ๋งํฌ๋งŒ ๋ณ€๊ฒฝ๋˜๊ณ  ๋‚˜๋จธ์ง€ ๋งํฌ๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค. ํŠน์ • ์ธ๋ฑ์Šค์— ๊ฐ์ฒด๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค. ArrayList๋Š” ์ค‘๊ฐ„ ์ธ๋ฑ์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ๋’ค์˜ ๊ฐ์ฒด๋Š” ์ธ๋ฑ์Šค๊ฐ€ 1์”ฉ ์•ž์œผ๋กœ ๋‹น๊ฒจ์ง„๋‹ค๊ณ  ํ–ˆ๋‹ค.

๋”ฐ๋ผ์„œ, ๋นˆ๋ฒˆํ•œ ๊ฐ์ฒด ์‚ญ์ œ์™€ ์‚ฝ์ž…์ด ์ผ์–ด๋‚˜๋Š” ๊ณณ์—์„œ๋Š” ArrayList๋ณด๋‹ค LinkedList๊ฐ€ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•œ๋‹ค. 

 

 

 

Map

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/HashMap.html

 

HashMap (Java SE 14 & JDK 14)

Type Parameters: K - the type of keys maintained by this map V - the type of mapped values All Implemented Interfaces: Serializable, Cloneable, Map Direct Known Subclasses: LinkedHashMap, PrinterStateReasons public class HashMap extends AbstractMap impleme

docs.oracle.com

ํ‚ค๊ฐ’์— ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

๋ฝ‘์•„ ์˜ฌ๋•Œ ์ˆœ์„œ๋Œ€๋กœ ๋ฝ‘์•„์˜ค์ง€ ์•Š๋Š”๋‹ค. ์ˆœ์„œ๋Œ€๋กœ ๋ฝ‘๊ณ  ์‹ถ์œผ๋ฉด? iterator

 

 

 

 

 

Properties

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Properties.html

 

Properties (Java SE 14 & JDK 14)

All Implemented Interfaces: Serializable, Cloneable, Map Direct Known Subclasses: Provider public class Properties extends Hashtable The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a st

docs.oracle.com

ArrayList๋ฅผ iterator๋กœ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ArrayList๋Š” ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•œ๋‹ค. 

๋ฐ˜์‘ํ˜•
Comments