GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java EnumSet complementOf() Method
Selected Reading
Java EnumSet complementOf() MethodDescriptionThe java.util.EnumSet.complementOf(EnumSet<E> s) method creates an enum set with the same element type as the specified enum set.It contains all the elements of this type that are not contained in the specified set. DeclarationFollowing is the declaration for java.util.EnumSet.complementOf() method public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) Parameterss − the enum set from whose complement to initialize this enum set. Return ValueThis method does not return any value. ExceptionNullPointerException − if s is null Getting Complement Set of EnumSet ExampleThe following example shows the usage of Java EnumSet complementOf(EnumSet) method to populate the EnumSet instance. We've created a enum Numbers. Then a EnumSet instance is created using a enum value. Using complementOf(EnumSet) method, enumSet is populated with remaining values of enum and resulted enumSet is printed. package com.tutorialspoint;
import java.util.EnumSet;
public class EnumSetDemo {
// create an enum
public enum Numbers {
ONE, TWO, THREE, FOUR, FIVE
};
public static void main(String[] args) {
// create an EnumSet that has only Numbers.FOUR as element
EnumSet<Numbers> set1 = EnumSet.of(Numbers.FOUR);
// print the set
System.out.println("Set1:" + set1);
// create a set2 which has all elements that set1 doesn't have
EnumSet<Numbers> set2 = EnumSet.complementOf(set1);
// print the updated set
System.out.println("Set2:" + set2);
}
}
OutputLet us compile and run the above program, this will produce the following result − Set1:[FOUR] Set2:[ONE, TWO, THREE, FIVE] Getting Complement Set of EnumSet of Enum with Values ExampleThe following example shows the usage of Java EnumSet complementOf(EnumSet) method to populate the EnumSet instance. We've created a enum Number. Then a EnumSet instance is created using a enum value. Using complementOf(EnumSet) method, enumSet is populated with remaining values of enum and resulted enumSet is printed. package com.tutorialspoint;
import java.util.EnumSet;
public class EnumSetDemo {
// create an enum
public enum Number {
ONE("1"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5");
// instance variable for enum
private String value;
public String getValue() {
return this.value;
}
// constructor should be private or protected
private Number(String value) {
this.value = value;
}
@Override
public String toString() {
return this.name() +"(" + this.value + ")";
}
};
public static void main(String[] args) {
// create an EnumSet that has only Numbers.FOUR as element
EnumSet<Number> set1 = EnumSet.of(Number.FOUR);
// print the set
System.out.println("Set1:" + set1);
// create a set2 which has all elements that set1 doesn't have
EnumSet<Number> set2 = EnumSet.complementOf(set1);
// print the updated set
System.out.println("Set2:" + set2);
}
}
OutputLet us compile and run the above program, this will produce the following result − Set1:[FOUR(4)] Set2:[ONE(1), TWO(2), THREE(3), FIVE(5)] java_util_enumset.htm
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |