Item 37. EnumMap
ordinal ์ธ๋ฑ์ฑ ๋์ EnumMap์ ์ฌ์ฉํ๋ผ.
๋ฐฐ์ด์ด๋ ๋ฆฌ์คํธ์์ ์์๋ฅผ ๊บผ๋ผ ๋ ordinal ๋ฉ์๋๋ก ์ธ๋ฑ์ค๋ฅผ ์ป์ ์ ์์ง๋ง, ๋ณดํต ์ด๋ฐ ์ฉ๋๋ก ordinal์ ์ฐ๋ ๊ฒ์ ์ข์ง ์๋ค.
ordinal
class Plant {
enum LifeCycle {ANNUAL, PERENNIAL, BIENNIAL}
final String name;
final LifeCycle lifeCycle;
Plant(String name, LifeCycle lifeCycle) {
this.name = name;
this.lifeCycle = lifeCycle;
}
@Override
public String toString() {
return name;
}
}
class Main {
public static void main(String[] args) {
Set<Plant>[] plantsByLifeCycle = (Set<Plant>[]) new Set[Plant.LifeCycle.values().length]; // ๋น๊ฒ์ฌ ํ๋ณํ ๊ฒฝ๊ณ
List<Plant> garden = List.of(
new Plant("๋ฐ์ง", Plant.LifeCycle.ANNUAL),
new Plant("์บ๋ฌ์จ์ด", Plant.LifeCycle.BIENNIAL),
new Plant("๋", Plant.LifeCycle.ANNUAL),
new Plant("๋ผ๋ฒค๋", Plant.LifeCycle.PERENNIAL),
new Plant("ํ์ฌ๋ฆฌ", Plant.LifeCycle.BIENNIAL),
new Plant("๋ก์ฆ๋ง๋ฆฌ", Plant.LifeCycle.PERENNIAL)
);
for (int i = 0; i < plantsByLifeCycle.length; i++) {
plantsByLifeCycle[i] = new HashSet<>();
}
for (Plant p : garden) {
plantsByLifeCycle[p.lifeCycle.ordinal()].add(p);
}
for (int i = 0; i < plantsByLifeCycle.length; i++) {
// ๋ฐฐ์ด์ ์ธ๋ฑ์ค์ ์๋ฏธ๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ ์ถ๋ ฅํ ๋ ๋ง๋ค LifeCycle.values()๋ฅผ ํธ์ถํด์ผ ํ๋ค.
System.out.printf("%s: %s%n", Plant.LifeCycle.values()[i], plantsByLifeCycle[i]); // ArrayIndexOutOfBoundsException ๋ฐ์ ๊ฐ๋ฅ
}
}
}
EnumMap
์ ๋ฐฉ์์ ๋์์ ํ์ง๋ง ์ฃผ์์ ์ ํ ๋๋ก ๋ฌธ์ ๊ฐ ๋ง๋ค. ์ ๋ฐฉ์์ด ์๋ EnumMap์ ์ฌ์ฉํ๋ฉด ์ด๋ฐ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
class Main {
public static void main(String[] args) {
Map<Plant.LifeCycle, Set<Plant>> plantsByLifeCycle = new EnumMap<>(Plant.LifeCycle.class); // ํ๋ณํ ์์ด ์์ ํ๊ฒ ์ ์ธ
List<Plant> garden = List.of(
new Plant("๋ฐ์ง", Plant.LifeCycle.ANNUAL),
new Plant("์บ๋ฌ์จ์ด", Plant.LifeCycle.BIENNIAL),
new Plant("๋", Plant.LifeCycle.ANNUAL),
new Plant("๋ผ๋ฒค๋", Plant.LifeCycle.PERENNIAL),
new Plant("ํ์ฌ๋ฆฌ", Plant.LifeCycle.BIENNIAL),
new Plant("๋ก์ฆ๋ง๋ฆฌ", Plant.LifeCycle.PERENNIAL)
);
for (Plant.LifeCycle lc : Plant.LifeCycle.values()) {
plantsByLifeCycle.put(lc, new HashSet<>());
}
for (Plant p : garden) {
plantsByLifeCycle.get(p.lifeCycle).add(p);
}
for (Plant.LifeCycle lifeCycle : plantsByLifeCycle.keySet()) {
// ํค๋ฅผ ์ง์ ์ฌ์ฉํด ์ํํ๋ฉด ordinal ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
System.out.printf("%s: %s%n", lifeCycle, plantsByLifeCycle.get(lifeCycle)); // ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ ์ถ๋ ฅ ๊ฐ๋ฅ
}
}
}
์ด์ ๊ฐ์ด EnumMap์ ์ฌ์ฉํ๋ฉด ์ฑ๋ฅ ์ ํ๋ ์๊ณ , ํ์ ์์ ์ฑ๋ ํ๋ณดํ ์ ์๋ค. (๋ด๋ถ์์ ๋ฐฐ์ด์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ด๋ฉฐ, ๋ด๋ถ ๊ตฌํ ๋ฐฉ์์ ์์ผ๋ก ์จ๊ฒจ์ ํ์ ์์ ์ฑ์ ํ๋ณดํ๋ค.)
์ด๊ฑฐ ํ์
๊ฐ๋ค ๋งคํ ์์
์ด๋ฒ์ ๋ ์ด๊ฑฐ ํ์ ๊ฐ๋ค์ ๋งคํํ์ฌ ๋ก์ง์ ๊ตฌํํด์ผํ๋ ๊ฒฝ์ฐ๋ฅผ ์ดํด๋ณด๋ฉด ์๋์ ๊ฐ์ด ๊ตฌํํ ์ ์๋ค.
enum Phase {
SOLID, LIQUID, GAS;
public enum Transition {
MELT, FREEZE, BOIL, CONDENSE, SUBLIME, DEPOSIT;
// ํ: from, ์ด: to, ์ํ๊ฐ ๋์ด๋๋ ๋งํผ ์ด์ฐจ์ ๋ฐฐ์ด์ด ์ปค์ง๋ค.
private static final Transition[][] TRANSITIONS = {
{null, MELT, SUBLIME},
{FREEZE, null, BOIL},
{DEPOSIT, CONDENSE, null}
};
// ๋ค๋ฅธ ์ํ๋ก ์ ์ดํ๋ ๋ฉ์๋
public static Transition from(Phase from, Phase to) {
// ordinal ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ธ๋ฑ์ค๋ฅผ ์ป๋๋ค, ์ด๋ ์์์ ์ดํด๋ณธ ๊ฒ์ฒ๋ผ ์ข์ง ์์ ๋ฐฉ์์ด๋ค.
return TRANSITIONS[from.ordinal()][to.ordinal()];
}
}
}
์์ ๊ฐ์ด ๊ตฌํํ๋ฉด ์ํ๊ฐ ๋์ด๋ ๋๋ง๋ค ์ด์ฐจ์ ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ ์ปค์ง๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๊ฐ ์ฌํด์ง๋ ๊ฒ ๋ฟ๋ง ์๋๋ผ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ๋ ๋์์ง๋ค.
enum Phase {
SOLID, LIQUID, GAS; // + PLASMA
public enum Transition {
MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID),
BOIL(LIQUID, GAS), CONDENSE(GAS, LIQUID),
SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID);
// + IONIZE(GAS, PLASMA), DEIONIZE(PLASMA, GAS);
// ์์ ์ด ๋งต ์ด๊ธฐํ
private static final Map<Phase, Map<Phase, Transition>> m = Stream.of(values())
.collect(groupingBy(t -> t.from,
() -> new EnumMap<>(Phase.class),
toMap(t -> t.to,
t -> t,
(x, y) -> y,
() -> new EnumMap<>(Phase.class))));
private final Phase from;
private final Phase to;
Transition(Phase from, Phase to) {
this.from = from;
this.to = to;
}
public static Transition from(Phase from, Phase to) {
return m.get(from).get(to);
}
}
}
ํ์ง๋ง EnumMap์ ์ฌ์ฉํ๋ฉด ์ด๊ธฐํ ๊ณผ์ ์ด ๋ค์ ๋ณต์กํด์ง์ง๋ง, ์ด์ฐจ์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค ํจ์ฌ ์์ ์ ์ด๊ณ ์ ์ฐํ๊ฒ ์ฌ์ฉํ ์ ์๋ค. ๋ง์ฝ ์๋ก์ด ์ํ์ธ PLASMA๊ฐ ์ถ๊ฐ๋๋๋ผ๋ ์ฃผ์๊ณผ ๊ฐ์ด ์ํ์ ์ ์ด ๋ชฉ๋ก์๋ง ์ถ๊ฐํ๊ณ , ๋๋จธ์ง ์ฝ๋๋ ์์ ํ ํ์๊ฐ ์๋ค.
Last updated
Was this helpful?