How to loop an enum in Java

Call the .values() method of the enum class to return an array, and loop it with the for loop: for (EnumClass obj : EnumClass.values()) { System.out.println(obj); } For Java 8, convert an enum into a stream and loop it: Stream.of(EnumClass.values()).forEach(System.out::println); 1. For Loop Enum 1.1 An enum to contain a list of the popular JVM …

Read more

Java – Compare Enum value

In Java, you can use == operator to compare Enum value. 1. Java Enum example Language.java package com.mkyong.java public enum Language { JAVA, PYTHON, NODE, NET, RUBY } 2. Compare with == Example to compare enum value with == operator. Test.java package com.mkyong.java public class Test { public static void main(String[] args) { // Covert …

Read more

Java – Convert String to Enum object

In Java, you can use Enum valueOf() to convert a String to an Enum object, review the following case study : 1. Java Enum example WhoisRIR.java package com.mkyong.whois.utils; public enum WhoisRIR { ARIN("whois.arin.net"), RIPE("whois.ripe.net"), APNIC("whois.apnic.net"), AFRINIC("whois.afrinic.net"), LACNIC("whois.lacnic.net"), JPNIC("whois.nic.ad.jp"), KRNIC("whois.nic.or.kr"), CNNIC("ipwhois.cnnic.cn"), UNKNOWN(""); private String url; WhoisRIR(String url) { this.url = url; } public String url() { …

Read more

Java enum example

Some of the Java enum examples, and how to use it, nothing special, just for self-reference. Note Consider the Enum type if your program consists of a fixed set of constants, like seasons of the year, operations calculator, user status and etc. 1. Basic Enum UserStatus.java public enum UserStatus { PENDING, ACTIVE, INACTIVE, DELETED; } …

Read more