Java Program to Remove Elements of an Enumset Collection That Does Not Exist in Another Enumset Collection

You are currently viewing Java Program to Remove Elements of an Enumset Collection That Does Not Exist in Another Enumset Collection

Java program to remove Elements of an EnumSet collection that does not exist in another EnumSet collection

// Java program to remove Elements of an EnumSet collection 
// that does not exist in another 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.GREEN);
    enumSet2.add(COLORS.BLUE);

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

    enumSet1.retainAll(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: [GREEN, BLUE]

Elements of enumSet1: [GREEN, BLUE]
Elements of enumSet2: [GREEN, BLUE]

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