Live-Study

[11์ฃผ์ฐจ] Enum

youn12 2021. 2. 19. 16:14
โœ๏ธ Info.

- Java Enum ํ•™์Šต

๐Ÿ“‹ List.

1. Enum ์ •์˜
2. Enum์ด ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ (values()์™€ valueOf()) + ordinal()(์ถ”๊ฐ€)
3. java.lang.Enum


โœ”๏ธ Content.

 

1. Enum ์ •์˜

 

1). Enum ์ด๋ž€

 

  • ๋ฉค๋ฒ„๋ผ ๋ถˆ๋ฆฌ๋Š” ๋ช…๋ช…๋œ ๊ฐ’์˜ ์ง‘ํ•ฉ์„ ์ด๋ฃจ๋Š” ์ž๋ฃŒํ˜•
  • ์—ด๊ฑฐํ˜•(enumerated type)์ด๋ผ ๋ถ€๋ฅธ๋‹ค.
  • ์„œ๋ฃŒ ์—ฐ๊ด€๋œ ์ƒ์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ
  • Java 1.5๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์—ด๊ฑฐ์ฒด๋ฅผ ๋น„๊ตํ•  ๋•Œ ์‹ค์ œ ๊ฐ’ ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ํƒ€์ž…๊นŒ์ง€๋„ ์ฒดํฌํ•œ๋‹ค.
  • ์—ด๊ฑฐ์ฒด์˜ ์ƒ์ˆ˜๊ฐ’์ด ์žฌ์ •์˜๋˜๋”๋ผ๋„ ์žฌ ์ปดํŒŒ์ผํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

2). Enum ์ •์˜

 

  • enum ์—ด๊ฑฐ์ฒด๋ช…{์ƒ์ˆ˜1์ด๋ฆ„, ์ƒ์ˆ˜2์ด๋ฆ„, ...}
public enum customEnum {
    RED, BLUE, ORANGE, YELLOW
}

 

3). Enum ์‚ฌ์šฉ

void useEunm(){
        customEnum red = customEnum.RED;
        System.out.println(red);
    }

 

์ถœ๋ ฅ

RED

 

4). Enum์˜ ์ƒ์ˆ˜๊ฐ’ ์ •์˜ ๋ฐ ์ถ”๊ฐ€

 

  • ์—ด๊ฑฐ์ฒด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ์ˆซ๊ฐ’์€ 0๋ถ€ํ„ฐ ์„ค์ •๋˜๋ฉฐ, ๊ทธ๋‹ค์Œ์€ ๋ฐ”๋กœ ์•ž์˜ ์ƒ์ˆซ๊ฐ’๋ณด๋‹ค 1๋งŒํผ ์ฆ๊ฐ€๋˜๋ฉฐ ์„ค์ •๋œ๋‹ค.
  • ๋ถˆ๊ทœ์น™ํ•œ ๊ฐ’์„ ์ƒ์ˆ˜๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ƒ์ˆ˜์˜ ์ด๋ฆ„ ์˜†์— ๊ด„ํ˜ธ()๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ด„ํ˜ธ ์•ˆ์— ์›ํ•˜๋Š” ์ƒ์ˆ˜๊ฐ’์„ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์ด๋•Œ ๋ถˆ๊ทœ์น™ํ•œ ํŠน์ •ํ•œ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ์ƒ์„ฑ์ž๋ฅผ ๋ณ„๋„๋กœ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.
public enum customEnum {
    RED(1), BLUE(2), ORANGE(5), YELLOW(6);

    int val;

    customEnum(int val) {
        this.val = val;
    }
}

 

5). Enum (val) ์‚ฌ์šฉ

void useEunm(){
        customEnum red = customEnum.RED;
        System.out.println(red);
        System.out.println(red.val);
    }

 

์ถœ๋ ฅ

RED
1

 

2. Enum์ด ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ (values()์™€ valueOf()) + ordinal()(์ถ”๊ฐ€)

 

1). values()

  • ํ•ด๋‹น ์—ด๊ฑฐ์ฒด์˜ ๋ชจ๋“  ์ƒ์ˆ˜๋ฅผ ์ €์žฅํ•œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
  • ์ž๋ฐ”์˜ ๋ชจ๋“  ์—ด๊ฑฐ์ฒด์— ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
// Enum
public enum customEnum {
    RED(1), BLUE(2), ORANGE(5), YELLOW(6);

    int val;

    customEnum(int val) {
        this.val = val;
    }
}

// values()
void valuesEnum(){
        customEnum[] values = customEnum.values();
        for (customEnum value : values) {
            System.out.println(value);
        }
    }

 

์ถœ๋ ฅ

RED
BLUE
ORANGE
YELLOW

 

2). valueOf()

 

  • ์ „๋‹ฌ๋œ ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•˜๋Š” ํ•ด๋‹น ์—ด๊ฑฐ์ฒด์˜ ์ƒ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
//Enum
public enum customEnum {
    RED(1), BLUE(2), ORANGE(5), YELLOW(6);

    int val;

    customEnum(int val) {
        this.val = val;
    }
}

//valueOf()
void valueOfEnum(){
        customEnum yellow = customEnum.valueOf("YELLOW");
        System.out.println(yellow);
    }

 

์ถœ๋ ฅ

YELLOW

 

3). ordinal()

 

  • ์—ด๊ฑฐ์ฒด ์ƒ์ˆ˜๊ฐ€ ์—ด๊ฑฐ์ฒด ์ •์˜์—์„œ ์ •์˜๋œ ์ˆœ์„œ(0๋ถ€ํ„ฐ ์‹œ์ž‘)๋ฅผ ๋ฐ˜ํ™˜
  • ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์€ ์—ด๊ฑฐ์ฒด ์ •์˜์—์„œ ํ•ด๋‹น ์—ด๊ฑฐ์ฒด ์ƒ์ˆ˜๊ฐ€ ์ •์˜๋œ ์ˆœ์„œ์ด๋ฉฐ, ์ƒ์ˆ˜๊ฐ’ ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋‹ค.
//Enum
public enum customEnum {
    RED(1), BLUE(2), ORANGE(5), YELLOW(6);

    int val;

    customEnum(int val) {
        this.val = val;
    }
}

//ordinal()
void ordinalEnum(){
        int ordinal = customEnum.YELLOW.ordinal();
        System.out.println(ordinal);
    }

 

์ถœ๋ ฅ

3

 

3. java.lang.Enum

 

  • Enum ํด๋ž˜์Šค๋Š” ๋ชจ๋“  ์ž๋ฐ” ์—ด๊ฑฐ์ฒด์˜ ๊ณตํ†ต๋œ ์กฐ์ƒ ํด๋ž˜์Šค์ด๋‹ค.
    • ๋”ฐ๋ผ์„œ Java๋Š” ๋‹ค์ค‘ ์ƒ์†์„ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— Enum์€ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์—†๋‹ค.
  • Enum ํด๋ž˜์Šค์—๋Š” ์—ด๊ฑฐ์ฒด๋ฅผ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์ด ์žˆ๋‹ค.
  • ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜ ๊ฐ„์˜ ๋น„๊ต์—๋Š” ==์™€ compareTo() ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค
    • ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€
  • ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ

 

๋ฉ”์†Œ๋“œ ์„ค๋ช…
static E values() ํ•ด๋‹น ์—ด๊ฑฐ์ฒด์˜ ๋ชจ๋“  ์ƒ์ˆ˜๋ฅผ ์ €์žฅํ•œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜
static E valueOf(String name) ์ „๋‹ฌ๋œ ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•˜๋Š” ํ•ด๋‹น ์—ด๊ฑฐ์ฒด์˜ ์ƒ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
String name() ํ•ด๋‹น ์—ด๊ฑฐ์ฒด ์ƒ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜
int ordinal() ํ•ด๋‹น ์—ด๊ฑฐ์ฒด ์ƒ์ˆ˜๊ฐ€ ์—ด๊ฑฐ์ฒด ์ •์˜์—์„œ ์ •์˜๋œ ์ˆœ์„œ(0๋ถ€ํ„ฐ ์‹œ์ž‘)์„ ๋ฐ˜ํ™˜

 

4. EnumSet

 

1).EnumSet

  • Set ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜์œผ๋กœ ์—ด๊ฑฐํ˜• ํƒ€์ž…์œผ๋กœ ์ง€์ •
  • java.util ํŒจํ‚ค์ง€ ํด๋ž˜์Šค
  • ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์ด์šฉํ•ด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„๋„ ์ ๊ฒŒ ์ฐจ์ง€ํ•˜๊ณ  ์†๋„๋„ ๋น ๋ฅด๋‹ค.
  • new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ ์ƒ์„ฑ ๋ถˆ๊ฐ€๋Šฅ

์ฃผ์˜์ 

 

  • ์ƒ์„ฑ์ž ์˜ค๋ฒ„ ๋ผ์ด๋”ฉ ๋ถˆ๊ฐ€๋Šฅ
  • ๋™์ผํ•œ ํƒ€์ž…์˜ ์—ด๊ฑฐ ์ƒ์ˆ˜๋งŒ ์„ ์–ธ ๊ฐ€๋Šฅ
  • null๊ฐ’์„ ๋„ฃ๊ฑฐ๋‚˜ NullPointException์„ ๋˜์งˆ ์ˆ˜ ์—†๋‹ค.
  • ์Šค๋ ˆ๋“œ๋กœ๋ถ€ํ„ฐ ์•ˆ์ „ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์™ธ๋ถ€์—์„œ ๋™๊ธฐํ™”
  • ์ƒ์ˆ˜๋Š” ์—ด๊ฑฐ ํ˜•์— ์„ ์–ธ๋œ ์ˆœ์„œ์— ๋”ฐ๋ผ ์ €์žฅ (ordinal)

๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ์„ค๋ช…
allof(Class<E> elementType) ๋ชจ๋“  ์ƒ์ˆ˜๋ฅผ ํฌํ•จํ•˜๋Š” EnumSet ์ƒ์„ฑ
clone() ๋ณต์‚ฌ๋ณธ ์ƒ์„ฑ
complementOf(EnumSet<E> s) ํŠน์ • ์ƒ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ  EnumSet ์ƒ์„ฑ
copyOf(Collection<E> c)
copyOf(EnumSet<E> s)
Collections์„ ์‚ฌ์šฉํ•œ ๋ณต์‚ฌ๋ณธ ์ƒ์„ฑ
noneOf(Class<E> elementType) ๋นˆ EnumSet ์ƒ์„ฑ
of(E e)
of(E first, E... rest)
of(E e1, E e2)
of(E e1, E e2, E e3)
of(E e1, E e2, E e3, E e4)
of(E e1, E e2, E e3, E e4, E e5)
ํŠน์ • ์ƒ์ˆ˜๋ฅผ ํฌํ•จํ•œ EnumSet ์ƒ์„ฑ
range(E form, E to) ํŠน์ • ๋ฒ”์œ„(์ƒ์ˆ˜)๋ฅผ ๊ฐ€์ง€๋Š” EnumSet ์ƒ์„ฑ

 

//Enum
public enum customEnum {
    RED(1, "๋นจ๊ฐ•"), BLUE(2, "ํŒŒ๋ž‘"), ORANGE(5, "์ฃผํ™ฉ"), YELLOW(6, "๋…ธ๋ž‘");

    int val;
    String name;

    customEnum() {
        this.val = val;
    }

    customEnum(int val, String name) {
        this.val = val;
        this.name = name;
    }
}

// SetEnum Method()
void setEnum(){
        /* ์ „์ฒด ์ƒ์ˆ˜๊ฐ€ ์žˆ๋Š” Enum Set ์ƒ์„ฑ */
        EnumSet<customEnum> allOf = EnumSet.allOf(customEnum.class);
        System.out.println("EnumSet.allOf => " + allOf);

        /* ๋ณต์‚ฌ๋ณธ ์ƒ์„ฑ */
        EnumSet<customEnum> clone = allOf.clone();
        System.out.println("clone => " + clone);

        /* EnumSet์„ ์ด์šฉํ•œ ๋ณต์‚ฌ๋ณธ ์ƒ์„ฑ */
        EnumSet<customEnum> copyOf = EnumSet.copyOf(allOf);
        System.out.println("copyOf => " + copyOf);

        /* ํŠน์ • ์ƒ์ˆ˜๋งŒ ์ƒ์„ฑ */
        EnumSet<customEnum> enumOf = EnumSet.of(customEnum.BLUE, customEnum.RED);
        System.out.println("EnumSet.of => " + enumOf);

        /* ํŠน์ • ์ƒ์ˆ˜ ์ œ์™ธํ•˜๊ณ  ์ƒ์„ฑ */
        EnumSet<customEnum> complementOf = EnumSet.complementOf(enumOf);
        System.out.println("EnumSet.complementOf = > " + complementOf);

        /* ๋นˆ Enum Set ์ƒ์„ฑ */
        EnumSet<customEnum> noneOf = EnumSet.noneOf(customEnum.class);
        System.out.println("EnumSet.noneOf => " + noneOf);

        /* ํŠน์ • ๋ฒ”์œ„ ์ƒ์„ฑ*/
        EnumSet<customEnum> range = EnumSet.range(customEnum.BLUE, customEnum.YELLOW);
        System.out.println("EnumSet.range => " + range);
    }

 

์ถœ๋ ฅ

 

EnumSet.allOf => [RED, BLUE, ORANGE, YELLOW]
clone => [RED, BLUE, ORANGE, YELLOW]
copyOf => [RED, BLUE, ORANGE, YELLOW]
EnumSet.of => [RED, BLUE]
EnumSet.complementOf = > [ORANGE, YELLOW]
EnumSet.noneOf => []
EnumSet.range => [BLUE, ORANGE, YELLOW]

โ—๏ธ Refer.

- enum ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•
https://ko.wikipedia.org/wiki/%EC%97%B4%EA%B1%B0%ED%98%95
https://opentutorials.org/course/2517/14151

- enum์ด ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ (values()์™€ valueOf())
http://www.tcpschool.com/java/java_api_enum

- java.lang.Enum

http://www.tcpschool.com/java/java_api_enum

- EnumSet
https://wisdom-and-record.tistory.com/52
https://velog.io/@kwj1270/Enum
https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html