Assume you need to optimize the code where set is an instance of Set and contains elements from the Element enum. Which Java class in the java.util package is better to use with enums instead of HashSet? Choose the best implementation class and use it to initialize the set instance instead of HashSet.
Enumset
The correct set
Report a typo
Sample Input 1:
Sample Output 1:
true
[FIRE, WIND, EARTH, SKY, WATER]Write a program in Java 17
import java.util.*;
public class Main {
enum Element {
FIRE, WIND, EARTH, SKY, WATER
}
public static void main(String[] args) {
// Change this statement
Set<Element> set = new HashSet<>();
/** instanceof operator returns true if set object has EnumSet type
* and false - otherwise
*/
System.out.println(set instanceof EnumSet<Element>);
System.out.println(set);
}
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.