๊ด€๋ฆฌ ๋ฉ”๋‰ด

โœ๐Ÿป๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ

Enum ๋ณธ๋ฌธ

Language/Java

Enum

์ฉ์‹œํ‚ด 2021. 1. 28. 18:43
728x90

๐Ÿ“ ์ž๋ฐ” ๋ผ์ด๋ธŒ ์Šคํ„ฐ๋”” 10์ฃผ ์ฐจ - Enum

1. Enum ์ถœ์—ฐ ์ด์ „

2. Enum ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

3. Enum์ด ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ

4. java.lang.Enum

5. EnumSet


์ด ๊ธ€์€ 23.05.07์— ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


1. Enum ์ถœ์—ฐ ์ด์ „

๊ณผ์ผ ๋ช…์— ์ˆซ์ž๋ฅผ ๋งค์นญํ•˜์—ฌ ๊ณผ์ผ์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

์ฃผ์„์ด ์žˆ์–ด์„œ 1๋ฒˆ์ด ์‚ฌ๊ณผ์ธ๊ฑด ์•Œ์•˜์ง€๋งŒ ๋งŒ์•ฝ ์ฃผ์„์ด ์ง€์›Œ์ง€๊ฑฐ๋‚˜, ๋ˆ„๊ตฐ๊ฐ€ ๋ชจ๋ฅด๊ณ  ์ˆ˜์ •ํ–ˆ๋‹ค๊ฑฐ๋‚˜, ์ฃผ์„๊ณผ ๊ณผ์ผ์˜ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ์–ด์„œ 1์ด ์‚ฌ๊ณผ์ธ ๊ฒƒ์„ ์•Œ๊ธฐ ํž˜๋“ค๋‹ค๋ฉด?

package enumTest;

public class ConstantDemo {
    public static void main(String[] args) {
        /**
         * 1. ์‚ฌ๊ณผ
         * 2. ๋ณต์ˆญ์•„
         * 3. ๋ฐ”๋‚˜๋‚˜
         */
        int type = 1;
        
        switch (type){
            case 1:
                System.out.println(57);
                break;
            case 2:
                System.out.println(34);
                break;
            case 3:
                System.out.println(93);
                break;
        }

    }
}

์†Œ์‹ค์˜ ์œ„ํ—˜์ด ์žˆ๋Š” ์ฃผ์„์—์„œ ํ•œ๋ฒˆ ์„ ์–ธํ•˜๋ฉด ๋” ์ด์ƒ ๋ฐ”๋€Œ์ง€ ์•Š์€ final ํ‚ค์›Œ๋“œ์™€ static ๋ณ€์ˆ˜(=ํด๋ž˜์Šค ๋ณ€์ˆ˜ : ๋ฉ”๋ชจ๋ฆฌ์— ํ•œ๋ฒˆ ํ• ๋‹น๋˜์–ด ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋  ๋•Œ ํ•ด์ œ๋จ)๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•ด์กŒ๋‹ค.

package enumTest;

public class ConstantDemo {

	private final static int APPLE = 1;
	private final static int PEACH = 2;
	private final static int BANANA = 3;

    public static void main(String[] args) {
        
        int type = 1;
        
        switch (type){
            case 1:
                System.out.println(57);
                break;
            case 2:
                System.out.println(34);
                break;
            case 3:
                System.out.println(93);
                break;
        }

    }
}

๊ณผ์ผ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค์ด ์ถ”๊ฐ€๋œ๋‹ค๋ฉด ์ฝ”๋“œ๋Š” ์ƒ์ˆ˜ ์„ ์–ธ์œผ๋กœ๋งŒ ๊ฐ€๋“ ์ฐฐ ๊ฒƒ์ด๋‹ค. Constant Interface๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฆฌํŒฉํ† ๋ง๋ณด์ž.

Constant Interface

์ƒ์ˆ˜๋งŒ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค. ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ฒฝ์šฐ ๋ณ€์ˆ˜ ๋“ฑ๋ก ์‹œ ์ž๋™์œผ๋กœ public static final์ด ๋ถ™๋Š”๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ ์–ด๋””์„œ๋‚˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. Constant Interface๋ฅผ Implemnetํ•  ๊ฒฝ์šฐ ์ƒ์† ๋ฐ›์€ ํด๋ž˜์Šค์—์„œ ์ƒ์ˆ˜ ๋ณ€์ˆ˜๋ช…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์„ ์–ธํ•˜๋ฉด์„œ APPLE์ด๋ผ๋Š” ๋™์ผํ•œ ์ƒ์ˆ˜๋ช… ์ปดํŒŒ์ผ๋Ÿฌ ์—๋Ÿฌ๋ฅผ ํ”ผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

package enumTest;

interface FRUIT{
    int APPLE = 1,PEACH = 2,  BANANA =3;
}

interface COMPANY{
    int APPLE = 1, GOOGLE = 2, ORACLE =3;
}

public class ConstantDemo {
        public static void main(String[] args) {

            if(FRUIT.APPLE == COMPANY.APPLE){
                System.out.println("๊ณผ์ผ์• ํ”Œ๊ณผ ๊ธฐ์—…์• ํ”Œ์ด ๊ฐ™๋‹ค๊ณ ??");
            }

            int type = 1;

            switch (type){
                case 1:
                    System.out.println(57);
                    break;
                case 2:
                    System.out.println(34);
                    break;
                case 3:
                    System.out.println(93);
                    break;
            }

        }
    }

ํ•˜์ง€๋งŒ FRUIT.APPLE๊ณผ COMPANY.APPLE๋Š” ๋‹ค๋ฅธ ๊ฐœ๋…์˜ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜์˜€์ง€๋งŒ ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ์ธ์‹( ๋™์ผํ•œ int ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋˜์–ด)๋˜๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ–ˆ๋‹ค.

 

Fruit, Company ํด๋ž˜์Šค ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋‹ค๋ฅด๊ฒŒ ๋ณ€๊ฒฝํ•˜์—ฌ ๋น„๊ต๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.

๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜๋Š” final์ด๋ผ ๋ถˆ๋ณ€์ด๊ณ , static์ด๋ฏ€๋กœ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๊ธธ์ด๋„ ๊ธธ๊ณ  ๋ณต์žกํ•˜๊ณ  ๋˜ํ•œ switch๋ฌธ์—์„œ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค. 

๊ทธ๋ฆฌํ•˜์—ฌ Enum์ด๋ผ๋Š” ๋“ฑ์žฅํ•˜์˜€๋‹ค..

package enumTest;

class Fruit{
     public static final Fruit APPLE = new Fruit();//์ž๊ธฐ ์ž์‹ ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์ˆ˜ APPLE์˜ ๊ฐ’์œผ๋กœ ์ง€์ •
     public static final Fruit PEACH = new Fruit();
     public static final Fruit BANANA = new Fruit();
}


class Company{
    public static final Fruit GOOGLE = new Fruit();
    public static final Fruit APPLE = new Fruit();
    public static final Fruit ORACLE = new Fruit();
}

public class ConstantDemo {
        public static void main(String[] args) {

            if(Fruit.APPLE == Company.APPLE){
                System.out.println("๊ณผ์ผ์• ํ”Œ๊ณผ ๊ธฐ์—…์• ํ”Œ์ด ๊ฐ™์•„์š”");
            }else{
                System.out.println("๊ณผ์ผ์• ํ”Œ๊ณผ ๊ธฐ์—…์• ํ”Œ์ด ๊ฐ™์ง€์•Š์•„์š”!!");
            }

            Fruit type = Fruit.APPLE;

            switch (type){
                case 1:
                    System.out.println(57);
                    break;
                case 2:
                    System.out.println(34);
                    break;
                case 3:
                    System.out.println(93);
                    break;
            }

        }
    }

2. Enum ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

Enum ์ด๋ž€?

* ์—ด๊ฑฐํ˜• (enumerated type) - ์„œ๋กœ ์—ฐ๊ด€๋œ ์ƒ์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ = ์ƒ์ˆ˜๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ํด๋ž˜์Šค

์œ„์˜ ์ฝ”๋“œ์—์„œ Fruit, Company๊ฐ€ ์ด์— ํ•ด๋‹นํ•œ๋‹ค.

 

Enum์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

  • ์ฝ”๋“œ๊ฐ€ ๋‹จ์ˆœํ•ด์ง„๋‹ค.
  • ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ์ƒ์†์„ ๋ฐฉ์ง€ํ•œ๋‹ค.
  • ํ‚ค์›Œ๋“œ enum์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌํ˜„์˜ ์˜๋„๊ฐ€ ์—ด๊ฑฐ์ž„์„ ๋ถ„๋ช…ํ•˜๊ฒŒ ๋‚˜ํƒ€ ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ์ˆ˜์ด๋ฏ€๋กœ ๊ฐ’์— ๋Œ€ํ•œ ๋™์  ํ• ๋‹น์ด ์•ˆ๋œ๋‹ค.
accessModifier enum enumName{
	//์ƒ์ˆ˜
	//๋ฉ”์„œ๋“œ ์ •์˜
}
package enumTest;
/*
* class Week{
*   public static final Week SUNDAY = new Week();
*   public static final Week MONDAY = new Week();
*   public static final Week TUESDAY = new Week();
*   public static final Week WEDNESDAY = new Week();
*   public static final Week THURSDAY = new Week();
*   public static final Week FRIDAY = new Week();
*   public static final Week SATURDAY = new Week();
*
*/

enum Week{
    SUNDAY(1,"First day"),//์ƒ์ˆ˜ ์„ ์–ธ ๋งˆ๋‹ค ์œ„์œผ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ธ์Šคํ„ด์Šคํ™”(์ƒ์„ฑ์ž ํ˜ธ์ถœ)
    MONDAY(2,"Second day"),
    TUESDAY(3,"Third day"),
    WEDNESDAY(4,"Fourth day"),
    THURSDAY(5,"FIfth day"),
    FRIDAY(6,"Sixth day"),
    SATURDAY(7,"Seventh day");

    private final int code;
    private final String stringCode;

    Week(int code, String stringCode){
        System.out.println("Call Constructor "+this);
        this.code = code;
        this.stringCode = stringCode;
        //์ „์—ญ๋ณ€์ˆ˜ stringCode = ๋งค๊ฐœ๋ณ€์ˆ˜ (=์ง€์—ญ๋ณ€์ˆ˜)stringCode๋ฅผ ๋Œ€์ž…
    }//์ธ์Šคํ„ด์Šค ํ™” ๋˜๋ฉด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
    //์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ์ „์—ญ๋ณ€์ˆ˜๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์œผ๋ฏ€๋กœ this๋ฅผ ์‚ฌ์šฉํ•˜์ง€์•Š์œผ๋ฉด ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ๋Œ€์ž…ํ•œ๋‹ค.

    public int getCode(){
        return code;
    }

    public String getStringCode(){
        return stringCode;
    }
}

public class ConstantDemo {
        public static void main(String[] args) {

            Week constantFromEnum = Week.MONDAY;
            System.out.println(constantFromEnum);
            System.out.println(constantFromEnum.getCode());
            System.out.println(constantFromEnum.getStringCode());
        }
    }

enum ์ž์ฒด๊ฐ€ ํด๋ž˜์Šค ์ด๋ฏ€๋กœ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

Call Constructor๊ฐ€ 7๋ฒˆ์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์€ enum Week ํด๋ž˜์Šค์˜ ํ•„๋“œ ์ˆ˜๋งŒํผ ํ˜ธ์ถœ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ๊ฐ์˜ ํ•„๋“œ๊ฐ€ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๋ฉด์„œ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค๋Š” ์ด์•ผ๊ธฐ

ํ•˜์ง€๋งŒ enum ์ƒ์„ฑ์ž์˜ ์ ‘๊ทผ์ œํ•œ์ž๋ฅผ public์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด๋Š” enum ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์˜ ์ ‘๊ทผ ์ œํ•œ์ž๋Š” private๋งŒ ํ—ˆ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์™ธ๋ถ€์—์„œ๋Š” enumํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•  ์ˆ˜ ์—†๋„๋ก ๋ง‰์•˜๋‹ค. enumํด๋ž˜์Šค๋Š” ์ œํ•œ๋œ ์ƒ์ˆ˜๊ฐ’์„ ๊ฐ€์ง€๋Š” ํด๋ž˜์Šค์ธ๋ฐ public ์ œํ•œ์ž๋กœ ๋ฒ”์œ„๋ฅผ ๋„“ํ˜”๋‹ค๋ฉด ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๊ฐ€ ๋‹ค๋ฅธ ์ƒ์ˆ˜๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ, enum ์ƒ์„ฑ์ž์— private ์ œํ•œ์ž๋ฅผ ๋ถ™์ด๋ฉด Modifier 'private' is redundant for enum constructors๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋‹ค. enum ํด๋ž˜์Šค ์ƒ์„ฑ์ž๊ฐ€ ์•”์‹œ์ ์œผ๋กœ private์—ฌ์„œ private ์ ‘๊ทผ ์ œํ•œ์ž๊ฐ€ ๋ถˆํ•„์š”ํ•˜๋‹ค๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ๋œฌ๊ฒƒ์ด๋‹ค.

-> ์ž๋ฐ”์˜ ์‹ ์—์„œ package-private, private ์ ‘๊ทผ ์ œํ•œ์ž๋งŒ enum ํด๋ž˜์Šค์˜ ์ ‘๊ทผ ์ œํ•œ์ž๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋‚˜์™€์žˆ๋Š”๋ฐ(p.331) ๊ธฐ๋ณธ์ ์œผ๋กœ private ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ์“ฐ๊ณ  ๊ทธ๊ฒŒ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— package-private์ด๋ผ๊ณ  ํ‘œํ˜„๋œ ๊ฒŒ ์•„๋‹๊นŒ?๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.


3. Enum์ด ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ

[์˜ˆ์ œ - Enumํด๋ž˜์Šค]

enum Week{
    SUNDAY(1,"First day"),
    MONDAY(2,"Second day"),
    TUESDAY(3,"Third day"),
    WEDNESDAY(4,"Fourth day"),
    THURSDAY(5,"FIfth day"),
    FRIDAY(6,"Sixth day"),
    SATURDAY(7,"Seventh day");

    private final int code;
    private final String stringCode;

    Week(int code, String stringCode){
        System.out.println("Call Constructor "+this);
        this.code = code;
        this.stringCode = stringCode;
    }

    public int getCode(){
        return code;
    }

    public String getStringCode(){
        return stringCode;
    }
}

compareTo()

enum์˜ ์ƒ์ˆ˜๊ฐ’์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

public class ConstantDemo {
        public static void main(String[] args) {

            Week constantFromEnum = Week.MONDAY;
            System.out.println(Week.SUNDAY.compareTo(Week.MONDAY);
   }
}

compareTo ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด๋ฉด ๋น„๊ต๋Œ€์ƒ์˜ ์ฐจ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

Enum์˜ ๋ฉ”์„œ๋“œ์™€ ์ƒ์ˆ˜๋ณ„๋กœ ๋‹ค๋ฅด๊ฒŒ ์žฌ์ •์˜ ๊ฐ€๋Šฅํ•œ ๋ฉ”์„œ๋“œ

valueOf()

๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ƒ์„ฑํ•œenum ํด๋ž˜์Šค๋Š” ๋ฌด์กฐ๊ฑด java.lang.Enumํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. ์ง์ ‘ extendsํ•˜์ง€ ์•Š์•„๋„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์ ์œผ๋กœ ์ƒ์†Œ๊ฐ‡๋Š”๋‹ค. valueOf()๋Š” ์ƒ์†๋ฐ›์€ java.lang.Enumํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ์ด๋ฉฐ, ์ „๋‹ฌ๋œ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€ Enumํด๋ž˜์Šค์˜ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

values()

Enumํด๋ž˜์Šค์—๋Š” API๋ฌธ์„œ์— ์—†๋Š” ํŠน์Šค ๋ฉ”์„œ๋“œ์ด๋ฉฐ, ์—ด๊ฑฐ๋œ ๋ชจ๋“  ์›์†Œ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

public enum Level {
	LOW(1,5),
	MIDDLE(3,10),
	HIGH(5,10);

	private int score;
	private int chance;

	Level(int score, int chance) {
		this.score = score;
		this.chance = chance;
	}

	public int getScore() {
		return score;
	}

	public int getChance() {
		return chance;
	}

	public static void main(String[] args) {
		Level level = Level.valueOf("MIDDLE");//์ „๋‹ฌ๋œ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€ enum ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜
		System.out.println("middle level score = " + level.getScore());
		System.out.println("middle level chance = " + level.getChance());

		Level[] values = Level.values();//ํ•ด๋‹น enum ํƒ€์ž…์˜ ์›์†Œ๋ฅผ ๋ฐฐ์—ดํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜
		for (Level lv : values) {
			System.out.println(lv + " level's score = " + lv.getScore());
		}
	}
}


4. java.lang.Enum

์ž๋ฐ”์—์„œ enum์€ java.lang ํŒจํ‚ค์ง€์˜ Enum ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.

enum ํด๋ž˜์Šค๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์ด๋‹ค. → Enum ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•œ ๊ฑฐ ๊ฐ™๋‹ค.

enum ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์„ ์–ธํ•œ enum ํด๋ž˜์Šค๋Š” Enum ํด๋ž˜์Šค๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์†ํ•˜์—ฌ, Enumํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜, ๋ฉ”์„œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ๋‹ค์ด์–ด๊ทธ๋žจ์„ ํ†ตํ•ด enum ํƒ€์ž…์˜ ํด๋ž˜์Šค๊ฐ€ Enum ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ์„ฑํ•œ enumํด๋ž˜์Šค์˜ ๋‹ค์ด์–ด๊ทธ๋žจ


5. EnumSet

Set ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜์˜ enum์—ด๊ฑฐ ์š”์†Œ set

์—ด๊ฑฐํ˜• ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•ด ๋†“์€ ์š”์†Œ๋“ค์„ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์š”์†Œ๋“ค์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ ์ œ๊ณต

package enumTest;

import java.util.EnumSet;

public class EnumSetTest {

    enum Day{
        SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
    }

    public static void main(String[] args) {

        EnumSet es = EnumSet.allOf(Day.class);
        EnumSet es2 = EnumSet.copyOf(es);//์ธ์ž์™€ ๋™์ผํ•œ enumSet ์ƒ์„ฑ =clone()
        System.out.println("EnumSet Day : "+es2);
        
        es2 = EnumSet.noneOf(Day.class);//EnumSet ๋น„์šฐ๊ธฐ
        System.out.println("EnumSet Day : "+es2);

        System.out.println("======================");
       
        es = EnumSet.of(Day.FRIDAY, Day.WEDNESDAY);//์ง€์ •ํ•œ ์š”์†Œ๋งŒ EnumSet์— ๋„ฃ๊ธฐ
        System.out.println("es : "+es);
        
        es2 = EnumSet.complementOf(es);//๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์˜ ์š”์†Œ๋“ค์„ ๋นผ๊ณ  EnumSet์„ ๊ตฌ์„ฑํ•ด๋ผ
        System.out.println("es2 : "+ es2);
        
        es2 = EnumSet.range(Day.TUESDAY, Day.FRIDAY);//๊ตฌ๊ฐ„์— ๋งž๊ฒŒ ์ถœ๋ ฅ
        System.out.println("es2 : "+es2);

    }
}

 

* allOf(Enum ํด๋ž˜์Šค๋ช…. class)

์—ด๊ฑฐํ˜•์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

 

* copyOf(es)

์ธ์ž์™€ ๋™์ผํ•œ enumSet ์ƒ์„ฑํ•œ๋‹ค.

 

* noneOf(Day.class)

EnumSet์„ ๋น„์šด๋‹ค.

 

* of(Day.FRIDAY, Day.WEDNESDAY)

์ง€์ •ํ•œ ์š”์†Œ๋งŒ EnumSet์— ๋„ฃ๋Š”๋‹ค.

 

* complementOf(es)

๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์˜ ์š”์†Œ๋“ค์„ ๋นผ๊ณ  EnumSet์„ ๊ตฌ์„ฑํ•œ๋‹ค.

 

* range(Day.TUESDAY, Day.FRIDAY);

๊ตฌ๊ฐ„์— ๋งž๊ฒŒ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋งŒ์•ฝ ์ˆœ์„œ๋ฅผ ๊ฑฐ๊พธ๋กœ ํ•ด์„œ range ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ ์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ IllegalArgumentException์ด ๋ฐœ์ƒํ•œ๋‹ค!

 

es2 = EnumSet.range(Day.FRIDAY, Day.TUESDAY);//๊ตฌ๊ฐ„์— ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅ
System.out.println("es2 Reverse : "+es2);


[ ์ฐธ๊ณ  ]

alecture.blogspot.com/2012/11/enumset.html

www.tcpschool.com/java/java_api_enum

javagoal.com/enum-in-java/

opentutorials.org/module/516/6091

www.opentutorials.org/module/1226/8025

https://www.geeksforgeeks.org/why-enum-class-can-have-a-private-constructor-only-in-java/

์ž๋ฐ”์˜ ์‹  13์žฅ

728x90
๋ฐ˜์‘ํ˜•

'Language > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JVM์˜ Garbage Collector  (0) 2022.10.28
Annotation ์• ๋…ธํ…Œ์ด์…˜  (1) 2021.02.04
Thread - ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ  (0) 2021.01.22
์˜ˆ์™ธ์ฒ˜๋ฆฌ  (0) 2021.01.14
[ JAVA ] ์ธํ„ฐํŽ˜์ด์Šค  (0) 2021.01.08