how to print all enum value in java?

前端 未结 6 1008
自闭症患者
自闭症患者 2021-01-03 21:49
enum generalInformation {
    NAME {
        @Override
        public String toString() {
            return \"Name\";
        }
    },
    EDUCATION {
        @Over         


        
相关标签:
6条回答
  • 2021-01-03 22:05

    values() on the enum returns an array. So, it would be simple to do the following to:

    System.out.println(Arrays.toString(generalInformation.values()));

    0 讨论(0)
  • 2021-01-03 22:09

    If you are still on Java 1.7 this is what worked for me:

    String genInfoValues = "";
    boolean firstRun = true;
    for (generalInformation info : generalInformation.values()){
        if (firstRun) {
            firstRun = false;
            genInfoValues += info.name();
        } else {
            genInfoValues += ", " + info.name();
        }
    }
    
    0 讨论(0)
  • 2021-01-03 22:15
    System.out.println(java.util.Arrays.asList(generalInformation.values()));
    

    Your second part... Just the same as an interface or a class

    0 讨论(0)
  • 2021-01-03 22:16

    In applications, it's good practice to separate data from presentation. It allows the data to be used in different user interfaces, it makes the data objects more lightweight, and it allows for the future possibility of internationalization.

    With that in mind, it's good to avoid strongly coupling the display name to the enum constant. Fortunately, there is a class which makes this easy: EnumMap.

    public class ApplicationUI {
        private final Map<GeneralInformation, String> names;
    
        public ApplicationUI() {
            names = new EnumMap<>(GeneralInformation.class);
    
            names.put(GeneralInformation.NAME,       "Name");
            names.put(GeneralInformation.EDUCATION,  "Education");
            names.put(GeneralInformation.EMAIL,      "Email");
            names.put(GeneralInformation.PROFESSION, "Profession");
            names.put(GeneralInformation.PHONE,      "Phone");
    
            assert names.keySet().containsAll(
                EnumSet.allOf(GeneralInformation.class)) :
                "Forgot to add one or more GeneralInformation names";
        }
    
        public String getNameFor(GeneralInformation info) {
            return names.get(info);
        }
    }
    
    0 讨论(0)
  • 2021-01-03 22:23

    Firstly, I would refactor your enum to pass the string representation in a constructor parameter. That code is at the bottom.

    Now, to print all enum values you'd just use something like:

    // Note: enum name changed to comply with Java naming conventions
    for (GeneralInformation info : EnumSet.allOf(GeneralInformation.class)) {
        System.out.println(info);
    }
    

    An alternative to using EnumSet would be to use GeneralInformation.values(), but that means you have to create a new array each time you call it, which feels wasteful to me. Admittedly calling EnumSet.allOf requires a new object each time too... if you're doing this a lot and are concerned about the performance, you could always cache it somewhere.

    You can use GeneralInformation just like any other type when it comes to parameters:

    public void doSomething(GeneralInformation info) {
        // Whatever
    }
    

    Called with a value, e.g.

    doSomething(GeneralInformation.PHONE);
    

    Refactoring using a constructor parameter

    public enum GeneralInformation {
        NAME("Name"),
        EDUCATION("Education"),
        EMAIL("Email"),
        PROFESSION("Profession"),
        PHONE("Phone");
    
        private final String textRepresentation;
    
        private GeneralInformation(String textRepresentation) {
            this.textRepresentation = textRepresentation;
        }
    
        @Override public String toString() {
             return textRepresentation;
        }
    }
    

    With your current values, you could actually just convert the name to title case automatically - but that wouldn't be very flexible for the long term, and I think this explicit version is simpler.

    0 讨论(0)
  • 2021-01-03 22:25

    Since Java 8 I would suggest the following solution:

    public static String printAll() {
        return Stream.of(GeneralInformation.values()).
                    map(GeneralInformation::name).
                    collect(Collectors.joining(", "));
    }
    
    0 讨论(0)
提交回复
热议问题