Java Program to Add Elements of an Enumset Collection to the Other Enumset Collection

You are currently viewing Java Program to Add Elements of an Enumset Collection to the Other Enumset Collection

Java program to add Elements of an EnumSet collection to the other EnumSet collection

// Java program to add Elements of an EnumSet collection 
// to the other EnumSet collection

import java.util.*;

//Enum for color constants
enum COLORS {
  RED,
  GREEN,
  BLUE,
  BLACK,
  WHITE
};

public class Main {
  public static void main(String[] args) {
    EnumSet < COLORS > enumSet1 = EnumSet.noneOf(COLORS.class);
    EnumSet < COLORS > enumSet2 = EnumSet.noneOf(COLORS.class);

    enumSet1.add(COLORS.RED);
    enumSet1.add(COLORS.GREEN);
    enumSet1.add(COLORS.BLUE);

    enumSet2.add(COLORS.BLACK);
    enumSet2.add(COLORS.WHITE);

    System.out.println("Elements of enumSet1: " + enumSet1);
    System.out.println("Elements of enumSet2: " + enumSet2);

    enumSet1.addAll(enumSet2);

    System.out.println("\nElements of enumSet1: " + enumSet1);
    System.out.println("Elements of enumSet2: " + enumSet2);
  }
}

Output

Elements of enumSet1: [RED, GREEN, BLUE]
Elements of enumSet2: [BLACK, WHITE]

Elements of enumSet1: [RED, GREEN, BLUE, BLACK, WHITE]
Elements of enumSet2: [BLACK, WHITE]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply