Saturday, 3 May 2014

EnumSet : complementOf

public static <E extends Enum<E>> EnumSet<E> complementOf (EnumSet<E> s)
Creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.

import java.util.*;

class EnumSetComplementOf{
 enum Day{
  MON,
  TUE,
  WED,
  THU,
  FRI,
  SAT,
  SUN;
 }
 
 public static void main(String args[]){
  EnumSet<Day> mySet1 = EnumSet.of(Day.MON, Day.TUE);
  EnumSet<Day> mySet2 = EnumSet.complementOf(mySet1);
  
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("Elements in mySet2 are");
  System.out.println(mySet2);
 }
}

Output
Elements in mySet1 are
[MON, TUE]
Elements in mySet2 are
[WED, THU, FRI, SAT, SUN]

1. throws NullPointerException if s is null
import java.util.*;

class EnumSetComplementOfNull{
 enum Day{
  MON,
  TUE,
  WED,
  THU,
  FRI,
  SAT,
  SUN;
 }
 
 public static void main(String args[]){
  EnumSet<Day> mySet = EnumSet.complementOf(null);
 }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.EnumSet.copyOf(EnumSet.java:146)
        at java.util.EnumSet.complementOf(EnumSet.java:185)
        at EnumSetComplementOfNull.main(EnumSetComplementOfNull.java:15)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment