tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Collection > EnumSet > EnumSet Complement

EnumSet Complement 

The following example shows how to create an enum set containing all the elements of this type that are not contained in the specified set. EnumSet provides the following APIs,

public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> paramClass);
 - Returns enum set containing all of the elements in the specified element type. 
 
public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> paramEnumSet)
 - Returns enum set containing all the elements of this type that are 
    not contained in the specified set. 


File Name  :  
com/bethecoder/tutorials/utils/enums/EnumSetComplementTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.enums;

import java.util.EnumSet;

public class EnumSetComplementTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    EnumSet<Fruit> enumSet = EnumSet.allOf(Fruit.class);
    System.out.println("All : " + enumSet);
    
    enumSet = EnumSet.of(Fruit.APPLE, Fruit.MANGO);
    System.out.println("Subset : " + enumSet);
    
    enumSet = EnumSet.complementOf(enumSet);
    System.out.println("Complement : " + enumSet);
  }

}
   


File Name  :  
com/bethecoder/tutorials/utils/enums/Fruit.java 
   
package com.bethecoder.tutorials.utils.enums;

public enum Fruit {
  APPLE,
  ORANGE,
  BANANA,
  CHERRY,
  MANGO
}
   

It gives the following output,
All : [APPLE, ORANGE, BANANA, CHERRY, MANGO]
Subset : [APPLE, MANGO]
Complement : [ORANGE, BANANA, CHERRY]



 
  


  
bl  br