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κ° μΆκ°λλλΌλ μ£Όμκ³Ό κ°μ΄ μνμ μ μ΄ λͺ©λ‘μλ§ μΆκ°νκ³ , λλ¨Έμ§ μ½λλ μμ ν νμκ° μλ€.