Java EnumSet Examples - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Saturday, February 4, 2017

Java EnumSet Examples

Java EnumSet Example

I have already discussed about how to use Java EnumSet and How to use some Java EnumSet method.

  1. Java EnumSet.of(E e)
  2. Java EnumSet.of(E e1, E e2)
  3. Java EnumSet.of(E e1, E e2, E e3)
  4. Java EnumSet.of(E e1, E e2, E e3, E e4)
  5. Java EnumSet.of(E e1, E e2, E e3, E e4, E e5)
  6. Java EnumSet.of(E first, E... rest)
  7. Java EnumSet.range(E from, E to)

Java EnumSet.of(E e)

In the following code shows how to use EnumSet.of(E e) method.

package com.walking.techie;

import java.util.EnumSet;

public class EnumSetOfDemo {

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

  public static void main(String[] args) {

    // create a enum set reference
    EnumSet<Days> day = null;

    // add one element
    day = EnumSet.of(Days.FRIDAY);
    System.out.println("Days.FRIDAY : " + day);
    // add another element which replace previous
    day = EnumSet.of(Days.MONDAY);
    System.out.println("Days.MONDAY : " + day);
  }
}

Output of above program is shown below:

Days.FRIDAY : [FRIDAY]
Days.MONDAY : [MONDAY]

Java EnumSet.of(E e1, E e2)

In the following code shows how to use EnumSet.of(E e1, E e2) method.

package com.walking.techie;

import java.util.EnumSet;

public class EnumSetOfDemo {

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

  public static void main(String[] args) {

    // create a enum set reference
    EnumSet<Days> day = null;

    // add two elements
    day = EnumSet.of(Days.FRIDAY, Days.MONDAY);
    System.out.println(day);
  }
}

Output of above program is shown below:

[MONDAY, FRIDAY]

Java EnumSet.of(E e1, E e2, E e3)

In the following code shows how to use EnumSet.of(E e1, E e2, E e3) method.

package com.walking.techie;

import java.util.EnumSet;

public class EnumSetOfDemo {

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

  public static void main(String[] args) {

    // create a enum set reference
    EnumSet<Days> day = null;

    // add three elements
    day = EnumSet.of(Days.FRIDAY, Days.MONDAY, Days.SATURDAY);
    System.out.println(day);
  }
}

Output of above program is shown below:

[MONDAY, FRIDAY, SATURDAY]

Java EnumSet.of(E e1, E e2, E e3, E e4)

In the following code shows how to use EnumSet.of(E e1, E e2, E e3, E e4) method.

package com.walking.techie;

import java.util.EnumSet;

public class EnumSetOfDemo {

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

  public static void main(String[] args) {

    // create a enum set reference
    EnumSet<Days> day = null;

    // add four elements
    day = EnumSet.of(Days.FRIDAY, Days.MONDAY, Days.SATURDAY, Days.SUNDAY);
    System.out.println(day);
  }
}

Output of above program is shown below:

[MONDAY, FRIDAY, SATURDAY, SUNDAY]

Java EnumSet.of(E e1, E e2, E e3, E e4, E e5)

In the following code shows how to use EnumSet.of(E e1, E e2, E e3, E e4, E e5) method.

package com.walking.techie;

import java.util.EnumSet;

public class EnumSetOfDemo {

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

  public static void main(String[] args) {

    // create a enum set reference
    EnumSet<Days> day = null;

    // add five elements
    day = EnumSet.of(Days.FRIDAY, Days.MONDAY, Days.SATURDAY, Days.SUNDAY, Days.TUESDAY);
    System.out.println(day);
  }
}

Output of above program is shown below:

[MONDAY, TUESDAY, FRIDAY, SATURDAY, SUNDAY]

Java EnumSet.of(E first, E... rest)

In the following code shows how to use EnumSet.of(E first, E... rest) method.

package com.walking.techie;

import java.util.EnumSet;

public class EnumSetOfDemo {

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

  public static void main(String[] args) {

    // create a enum set reference
    EnumSet<Days> days = null;

    Days[] dayOfArray = {Days.WEDNESDAY, Days.THURSDAY, Days.FRIDAY};
    // add elements using variable args
    days = EnumSet.of(Days.TUESDAY, dayOfArray);
    System.out.println(days);
  }
}

Output of above program is shown below:

[TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]

Java EnumSet.range(E from, E to)

In the following code shows how to use EnumSet.range(E from, E to) method.

package com.walking.techie;

import java.util.EnumSet;

public class EnumSetOfDemo {

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

  public static void main(String[] args) {

    // create a enum set reference
    EnumSet<Days> days = null;
    // elements from TUESDAY to SUNDAY
    days = EnumSet.range(Days.TUESDAY, Days.SUNDAY);
    System.out.println(days);
  }
}

Output of above program is shown below:

[TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]

No comments :

Post a Comment