If you need to create groups for your Enum class, there are few options which you can choose. Here is examples of two ways.
Inner Enum class
public class EnumGroups { public enum Fields{ USER_NAME(Group.USER_BASIC), USER_SURNAME(Group.USER_BASIC), USER_EMAIL(Group.USER_BASIC), USER_PHONE(Group.USER_BASIC), USER_STREET(Group.USER_ADDRESS), USER_ZIP(Group.USER_ADDRESS), USER_COUNTRY(Group.USER_ADDRESS), USER_NEWS(Group.USER_SETTINGS), USER_PRODUCT(Group.USER_SETTINGS); Fields(Group group) { this.group = group; } private final Group group; public Group getGroup() { return this.group; } public enum Group { USER_BASIC, USER_ADDRESS, USER_SETTINGS; } } public static void main(String[] args) { for(Fields field : Fields.values()) { System.out.println(field.name() + " is under " + field.getGroup().name()); } } }
Output:
USER_NAME is under USER_BASIC USER_SURNAME is under USER_BASIC USER_EMAIL is under USER_BASIC USER_PHONE is under USER_BASIC USER_STREET is under USER_ADDRESS USER_ZIP is under USER_ADDRESS USER_COUNTRY is under USER_ADDRESS USER_NEWS is under USER_SETTINGS USER_PRODUCT is under USER_SETTINGS
Use EnumSet
import java.util.EnumSet; public class EnumGroups { public enum Fields{ USER_NAME, USER_SURNAME, USER_EMAIL, USER_PHONE, USER_STREET, USER_ZIP, USER_COUNTRY, USER_NEWS, USER_PRODUCT; public static EnumSet<Fields> userBasic = EnumSet.of(USER_NAME, USER_SURNAME, USER_EMAIL, USER_PHONE); public static EnumSet<Fields> userAddress = EnumSet.of(USER_STREET, USER_ZIP, USER_COUNTRY); public static EnumSet<Fields> userSetting = EnumSet.of(USER_NEWS, USER_PRODUCT); } public static void main(String[] args) { for(Fields field : Fields.values()) { if (Fields.userBasic.contains(field)) System.out.println(field.name() + " is from User basic information part."); if (Fields.userAddress.contains(field)) System.out.println(field.name() + " is from User address information part."); if (Fields.userSetting.contains(field)) System.out.println(field.name() + " is from User setting information part."); } } }
Output:
USER_NAME is from User basic information part. USER_SURNAME is from User basic information part. USER_EMAIL is from User basic information part. USER_PHONE is from User basic information part. USER_STREET is from User address information part. USER_ZIP is from User address information part. USER_COUNTRY is from User address information part. USER_NEWS is from User setting information part. USER_PRODUCT is from User setting information part.
This site is really cool. I have bookmarked it. Do you allow guest post
on your blog ? I can provide hi quality posts for you.
Let me know.
Hi, thank you! I don’t allow it, but if you really want to, you can Sing up as user (default role is only subscriber) and I’ll give you rights for creating posts (keep on mind that I have to approve all posts before publishing). Let me know about your decision by creating registration.