Java

[Effective Java] ์•„์ดํ…œ 37. ordinal ์ธ๋ฑ์‹ฑ ๋Œ€์‹  EnumMap์„ ์‚ฌ์šฉํ•˜๋ผ.

quedevel 2023. 3. 21. 10:39
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 37. ordinal ์ธ๋ฑ์‹ฑ ๋Œ€์‹  EnumMap์„ ์‚ฌ์šฉํ•˜๋ผ.

public 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;
    }
}
  • ordinal()์„ ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉ - ๋”ฐ๋ผ ํ•˜์ง€ ๋ง ๊ฒƒ!
public static void main(String[] args) {
  Plant[] garden = {
          new Plant("๋ฐ”์งˆ",    LifeCycle.ANNUAL),
          new Plant("์บ๋Ÿฌ์›จ์ด", LifeCycle.BIENNIAL),
          new Plant("๋”œ",      LifeCycle.ANNUAL),
          new Plant("๋ผ๋ฒค๋”",   LifeCycle.PERENNIAL),
          new Plant("ํŒŒ์Šฌ๋ฆฌ",   LifeCycle.BIENNIAL),
          new Plant("๋กœ์ฆˆ๋งˆ๋ฆฌ", LifeCycle.PERENNIAL)
  };

  // ์ฝ”๋“œ 37-1 ordinal()์„ ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉ - ๋”ฐ๋ผ ํ•˜์ง€ ๋ง ๊ฒƒ! (226์ชฝ)
  Set<Plant>[] plantsByLifeCycleArr = (Set<Plant>[]) new Set[Plant.LifeCycle.values().length];
  for (int i = 0; i < plantsByLifeCycleArr.length; i++){
      plantsByLifeCycleArr[i] = new HashSet<>();
  }
  for (Plant p : garden) {
      plantsByLifeCycleArr[p.lifeCycle.ordinal()].add(p);
  }
  // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
  for (int i = 0; i < plantsByLifeCycleArr.length; i++) {
      System.out.printf("%s: %s%n", Plant.LifeCycle.values()[i], plantsByLifeCycleArr[i]);
  }
  }
์ถœ๋ ฅ ๊ฒฐ๊ณผ

๋™์ž‘์€ ํ•˜์ง€๋งŒ ๋ฌธ์ œ๊ฐ€ ํ•œ๊ฐ€๋“์ด๋‹ค. ๋ฐฐ์—ด์€ ์ œ๋„ค๋ฆญ๊ณผ ํ˜ธํ™˜๋˜์ง€ ์•Š์œผ๋‹ˆ ๋น„๊ฒ€์‚ฌ ํ˜•๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๊ณ  ๊น”๋”ํžˆ ์ปดํŒŒ์ผ๋˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค.

๋”ฐ๋ผ์„œ, ์—ด๊ฑฐ ํƒ€์ž…์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค๊ณ„ํ•œ ์•„์ฃผ ๋น ๋ฅธ Map ๊ตฌํ˜„์ฒด๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, ๋ฐ”๋กœ EnumMap์ด๋‹ค.


  • EnumMap์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ์™€ ์—ด๊ฑฐ ํƒ€์ž…์„ ๋งคํ•‘ํ•œ๋‹ค.
Map<LifeCycle, Set<Plant>> plantsByLifeCycle = new EnumMap<>(Plant.LifeCycle.class);
for (Plant.LifeCycle lc : Plant.LifeCycle.values()) {
  plantsByLifeCycle.put(lc, new HashSet<>());
}
for (Plant p : garden) {
  plantsByLifeCycle.get(p.lifeCycle).add(p);
}
System.out.println(plantsByLifeCycle);
์ถœ๋ ฅ ๊ฒฐ๊ณผ

๋” ์งง๊ณ  ๋ช…๋ฃŒํ•˜๊ณ  ์•ˆ์ „ํ•˜๊ณ  ์„ฑ๋Šฅ๋„ ์›๋ž˜ ๋ฒ„์ „๊ณผ ๋น„๋“ฑํ•˜๋‹ค. ์•ˆ์ „ํ•˜์ง€ ์•Š์€ ํ˜•๋ณ€ํ™˜์€ ์“ฐ์ง€ ์•Š๊ณ ,

๋งต์˜ ํ‚ค์ธ ์—ด๊ฑฐ ํƒ€์ž…์ด ๊ทธ ์ž์ฒด๋กœ ์ถœ๋ ฅ์šฉ ๋ฌธ์ž์—ด์„ ์ œ๊ณตํ•˜๋‹ˆ ์ถœ๋ ฅ ๊ด€ใ„น์— ์ง์ ‘ ๋ ˆ์ด๋ธ”์„ ๋‹ฌ ์ผ๋„ ์—†๋‹ค.

  • ์ŠคํŠธ๋ฆผ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ
System.out.println(Arrays.stream(garden).collect(groupingBy(p -> p.lifeCycle)));
System.out.println(Arrays.stream(garden).collect(groupingBy(p -> p.lifeCycle, () -> new EnumMap<>(LifeCycle.class), toSet())));

๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ordinal์„ ์“ฐ๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ข‹์ง€ ์•Š์œผ๋‹ˆ, ๋Œ€์‹  EnumMap์„ ์‚ฌ์šฉํ•˜๋ผ.๋‹ค์ฐจ์› ๊ด€๊ณ„๋Š” EnumMap<...,EnumMap<...>>์œผ๋กœ ํ‘œํ˜„ํ•˜๋ผ. "์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” Enum.ordinal์„ (์›ฌ๋งŒํ•ด์„œ๋Š”) ์‚ฌ์šฉํ•˜์ง€ ๋ง์•„์•ผํ•œ๋‹ค."


์ฐธ๊ณ  ์ž๋ฃŒ

Joshua Bloch, ใ€ŽEffective Java 3/Eใ€, ๊ฐœ์•ž๋งต์‹œ ์˜ฎ๊น€, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ์‚ฌ์ดํŠธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=

728x90
๋ฐ˜์‘ํ˜•