0. Set ์ธํฐํ์ด์ค ํน์ง
- Collection ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์
- ์์์ ์ค๋ณต์ ํ์ฉํ์ง ์์
- ์์ ์์(์ ์ฅ์์ ์ ์ง ์๋จ) โก get ๋ฉ์๋ ๋์ ์ iterator๋ฅผ ํตํด์ ๊ฐ ์์์ ์ ๊ทผํด์ผ ํ๋ค. (์ธ๋ฑ์ค๋ฅผ ํตํ ์ ๊ทผ ๋ถ๊ฐ)
์ฌ์ฉ์ ์ฃผ์ํด์ผํ ์
Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.
๊ฐ๋ณ ๊ฐ์ฒด๋ฅผ Set์ ์์๋ฅผ ์ฌ์ฉํ ๋๋ ์ฃผ์ํด์ผ ํ๋ค. ์งํฉ ์์์ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ ๊ฒฝ์ฐ ๊ฐ ๋๋ฑ์ฑ ๋น๊ต์ ์ํฅ์ ๋ฏธ์ณ Set์ ์ ์์ ๋์์ด ๋ณด์ฅ๋์ง ์์ ์ ์๋ค.
Set์ ๊ตฌํ๋ง๋ค ํฌํจ๋ ์ ์๋ ์์์ ์ ํ์ด ๋ค๋ฅด๋ค. ์ผ๋ถ ๊ตฌํ์์๋ null ์์๋ฅผ ์ ํํ๊ธฐ๋ ํ๊ณ , ์ผ๋ถ์์๋ Type์ ์ ํ์ ๋๊ธฐ๋ ํ๋ค. ์ด๋ฌํ ์ ํ๋ ์์๋ฅผ ์ถ๊ฐํ๋ฉด NullPointerException ๋๋ ClassCastException์ด ๋ฐ์ํ๋ค.
1. Set ์ธํฐํ์ด์ค์ ๊ตฌํ
- List
- SortedSet
- HashSet
- TreeSet
- AbstractSet
2. Set์ ๊ธฐ๋ณธ ๋ฉ์๋
return value | method | description |
int | size() | Set์ ์์ ์๋ฅผ ๋ฐํํ๋ค. ์์ ์๊ฐ Integer.MAX_VALUE ์ด์์ด๋ฉด Integer.MAX_VALUE ๋ฐํํ๋ค. |
boolean | isEmpty() | Set์ ์์๊ฐ ํฌํจ๋์ด ์์ง ์๋ค๋ฉด true๋ฅผ ๋ฐํํ๋ค. |
boolean | contains(Object o) | Set์ ์ง์ ๋ ์์๊ฐ ํฌํจ๋์ด ์๋ค๋ฉด true๋ฅผ ๋ฐํํ๋ค. |
Iterator<E> | iterator() | Set์ ์์๋ฅผ ์ํํ ์ ์๋ Iterator ๊ตฌํ ๊ฐ์ฒด๊ฐ ๋ฐํ๋๋ค. iterator๋ฅผ ํตํด์ ์์๋ค์ ์ํํ ๋ ํน๋ณํ ์์์์ด ์ํํ๋ค. |
Object[] | toArray() | Set ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณํ |
<T> T[] | toArray(T[] a) | Set ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณํ(๋ณํ๋ ๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌ) |
boolean | add(E e) | ์ง์ ํ ์์๊ฐ ์์ง Set์ ์๋ ๊ฒฝ์ฐ Set์ ์ง์ ํ ์์๋ฅผ ์ถ๊ฐํ๋ค.(equals ๋ฉ์๋๋ก Set ๋ด ๋ฐ์ดํฐ์ ๋์ผ์ฑ ์ฌ๋ถ ๊ฒ์ฌ) ์ง์ ํ ์์๊ฐ ์ด๋ฏธ ์๋ ๊ฒฝ์ฐ false๋ฅผ ๋ฐํํ๊ณ ์งํฉ์ ์์๋ฅผ ์ถ๊ฐํ์ง ์๋๋ค. |
boolean | remove(Object o) | ์ง์ ํ ์์๊ฐ ์ด๋ฏธ Set์ ์๋ ๊ฒฝ์ฐ ํด๋น ์์๋ฅผ ์ ๊ฑฐํ๋ค. (equals ๋ฉ์๋๋ก Set ๋ด ๋ฐ์ดํฐ์ ๋์ผ์ฑ ์ฌ๋ถ ๊ฒ์ฌ) ์ง์ ํ ์์๊ฐ Set์ ํฌํจ๋ ๊ฒฝ์ฐ true๋ฅผ ๋ฐํํ๋ค. |
boolean | containsAll(Collection<?> c) | ์ง์ ํ Collection์ ๋ชจ๋ ์์๊ฐ Set์ ํฌํจ๋ ๊ฒฝ์ฐ true๋ฅผ ๋ฐํํ๋ค. |
boolean | addAll(Collection<? extends E> c) | ์ง์ ํ Collection์ ๋ชจ๋ ์์๊ฐ Set์ ์์ง ์๋ ๊ฒฝ์ฐ Set์ ์ถ๊ฐ๋๋ค. ํฉ์งํฉ(union)์ฒ๋ผ ์ํ๋๋ค. |
boolean | retainAll(Collection<?> c) | ์ง์ ํ Collection์ ํฌํจ๋ Set ๋ด ์์๋ง ์ ์งํ๋ค. ๊ต์งํฉ(intersection)์ฒ๋ผ ์ํ๋๋ค. |
boolean | removeAll(Collection<?> c) | ์ง์ ํ Collection์ ํฌํจ๋ ๋ชจ๋ ์์๋ฅผ Set์์ ์ ๊ฑฐํ๋ค. ์ฐจ์งํฉ(asymmetric set difference)์ฒ๋ผ ์ํ๋๋ค. |
void | clear() | Set์ ์๋ ๋ชจ๋ ์์๋ฅผ ์ ๊ฑฐํ๋ค. |
Set<E> | copyOf(Collection<? extends E> coll) | ์ง์ ํ ์ปฌ๋ ์
์ ์์๋ฅผ ํฌํจํ๋ Unmodifiable Set์ ๋ฐํํ๋ค. ์ง์ ํ ์ปฌ๋ ์
์ null์ผ ์ ์์ผ๋ฉฐ null ์์๋ฅผ ํฌํจํ ์ ์๋ค. copyOf๋ ์๋ณธ๊ณผ์ ์ฐธ์กฐ๋ ๋๊ธฐ๋ ๊ฒ์ด๋ฏ๋ก ์ง์ ๋ ์ปฌ๋ ์ ์ด ์์ ๋๋๋ผ๋ ๋ฐํ๋ Unmodifiable Set์ ๋ฐ์์ด ์๋๋ค. |
์ด์ธ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋์ธ of ๊ฐ ์๋ค. of ๋ฉ์๋์ ์ฃผ์์ ๋ณด๋ฉด ์๋์ ๊ฐ์ ๋ฉ์์ง๋ฅผ ๋ณผ ์ ์๋๋ฐ Unmodifiable Set ๊ฐ๋ ์ด ์ดํด๊ฐ ์๋๋ค. (1) unmodifiable ํ๋ค๋ ๊ฒ์ด ์ด๋ค ์๋ฏธ์ธ์ง ์์๋ณด์.
- Unmodifiable
- ๋ง๊ทธ๋๋ก ์์ ํ ์ ์๋ค๋ ๊ฒ์ผ๋ก ํน์ ์ปฌ๋ ์ ์ ๋ํด ์ฝ๊ธฐ ์ด์ธ์ ๋ค๋ฅธ ์์ ์ ํ๊ฒ ๋๋ฉด UnsupportedOperationException ์์ธ๊ฐ ๋ฐ์ํ๋ค.
- ์๋ณธ์ ๋ํ ์ฐธ์กฐ๋ ๊ฐ์ง๊ณ ์๋ ์์ ์ ๋ํด์๋ ๋ง๊ณ ์ถ์ ๊ฒฝ์ฐ ํ์ฉํ ์ ์๋ค. ์๋ณธ์ ๋ํ ์ฐธ์กฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ ์๋ก์ด ๋ฐํ๋ ๊ฐ์ฒด๋ฅผ ํตํด ์์ ์ ๋ถ๊ฐํ์ง๋ง ์๋ณธ์ด ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ unmodifiable collection์ ํตํด์ ์์ ๋ ๊ฐ๋ ์ฐธ์กฐ๊ฐ ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ด๋ค.
Collections ํด๋์ค์์ unmodifiable wrapper์ ๋ํ ์ฃผ์ ๋ด์ฉ ๋ฐ์ท
Returns an unmodifiable view of the specified collection. Query operations on the returned collection "read through" to the specified collection, and attempts to modify the returned collection, whether direct or via its iterator, result in an UnsupportedOperationException.
๋ of ๋ฉ์๋๊ฐ ํ๋ผ๋ฏธํฐ๊ฐ 0๊ฐ๋ถํฐ 10๊ฐ๊น์ง ์ค๋ฒ๋ก๋ฉ ๋ ๊ฒ์ ๋ณผ ์ ์๋๋ฐ, (2) ์ ๊ตณ์ด 10๊ฐ๊น์ง๋ง ์ค๋ฒ๋ก๋ฉ์ ํ์๊น? ์ ๋ํ ๊ถ๊ธ์ฆ์ด ์๊ฒผ๋ค. ๋จ์ํ๊ฒ ์๊ฐํ์๋ ์ฑ๋ฅ์์ ๋ฌธ์ ์ธ๊ฒ ๊ฐ์๋ฐ ์ถํ ๋ค๋ฅธ ๊ธ์์ ๋ ์์ธํ ์ ๋ฆฌํด๋ณด๊ฒ ๋ค.
3. HashSet
- HashMap์ ํด์ํ
์ด๋ธ์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ Set ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ค.
- ์ ์ฅํ ๊ฐ์ฒด์ ํด์๊ฐ์ hashCode() ๋ฉ์๋๋ฅผ ํตํด ๊ตฌํ๊ณ ์ ์ฅ๋์ด ์๋ ํด์๊ฐ๋ค๊ณผ ๋น๊ตํ๋ค.
- ๋์ผํ ํด์๊ฐ์ด ์๋ ๊ฒฝ์ฐ equals() ๋ฉ์๋๋ฅผ ํตํด ๋์ผํ ๊ฐ์ฒด๊ฐ ์๋ ๊ฒฝ์ฐ ์ค๋ณต ์ ์ฅํ์ง ์๋๋ค.
- ์ด๋ฏธ hashCode(), equals() ๋ฉ์๋๊ฐ ์ ์ฅ๋์ด ์๋ค๋ฉด ์๊ด์์ง๋ง ์ฌ์ฉ์ ์ ์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ฐ๋์ ํด๋น ๋ฉ์๋๋ค์ ๊ตฌํํด์ค์ผ ํ๋ค.
- null ์์๋ฅผ ํ์ฉ
- ์ ๋ ฅ ์์ ๋ณด์ฅ ์ํจ
- ์์ ์ค๋ณต ์์
- ๋๊ธฐํ๋ฅผ ๋ณด์ฅํ์ง ์๋๋ค.
- ๋๊ธฐํ๋ฅผ ๋ณด์ฅํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด Wrappingํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
Set s = Collections.synchronizedSet(new HashSet(...));
4. LinkedHashSet
- HashSet๊ณผ ๋ฌ๋ฆฌ ์ ๋ ฅ ์์๋ฅผ ๋ณด์ฅํ๋ค. โก ์์๋ฅผ ์ ์ฅํด์ผ ํ๋ ๊ฒฝ์ฐ ์ ์ฉ
- ์์ ์ค๋ณต ์์
5. TreeSet
- TreeMap์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ Set ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ค. (์ด์งํธ๋ฆฌ ๊ธฐ๋ฐ)
- ์์๋ค์ ์ ๋ ฌ ์ํ๋ฅผ ์ ์งํ๋ค. โก ์์ฐ์์์ด๊ฑฐ๋ Comparator๋ฅผ ์ ๊ณตํ๋ค๋ฉด ์ ์๋ ๋น๊ต๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ ฌ๋๋ค.
- add, remove, contains ๋ฉ์๋ ์ฌ์ฉ์ O(logN)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ค.
- ์์ ์ค๋ณต ์์
- ๋๊ธฐํ๋ฅผ ๋ณด์ฅํ์ง ์๋๋ค.
- ๋๊ธฐํ๋ฅผ ๋ณด์ฅํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด Wrappingํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
'๊ฐ๋ฐ > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Collection Framework - List์ ๊ตฌํ์ฒด(๊ธฐ๋ณธ) (0) | 2023.04.29 |
---|---|
[Java] ์ถ์ํด๋์ค์ ์ธํฐํ์ด์ค (0) | 2023.04.26 |
[Java] Collection Framework - Map์ ๊ตฌํ์ฒด(์ฌํ) (0) | 2023.04.21 |
[Java] Collection Framework - Map์ ๊ตฌํ์ฒด(๊ธฐ๋ณธ) (2) | 2023.04.21 |