Home > Enterprise >  Add different enum classes to a list
Add different enum classes to a list

Time:11-16

Lets say I have the below enums declared

public class Enums{
    public enum A{
        a1,
        a2;
    }
    public enum B{
        b1,
        b2;
    }
    public enum C{
        c1,
        c2;
    }
}

Now I want to pass a1,b1 and c1 to a constructor while creation of object

Course c = new Course(a1,b1,c1);

How can pass these values like a list instead of typing all the enums. Can I do as below?


List<Enums> eValues = new ArrayList<Enums>();
eValues.add(A.valueOf("a1"));
eValues.add(B.value("b1"));
eValues.add(C.value("c1"));

//and then can I do as below?

Course c = new Course(eValues);

I am getting an error "no suitable method found for add(A)" while adding elements to list

Code on the Constructor side:

public <T extends Enum<T>>Course(T[] eValues){
//some processing using those enums
}

Need help on how to add enums to a list and send it while object creation? and if possible how to receive them in the constructor

CodePudding user response:

None of this works. A is not an Enums - the fact that A is declared inside Enums does not mean: Enums x = A.a1; is legal. Try it. Enums can implement interfaces; they cannot extend classes.

You can have a method or constructor that accepts any number of values, using varargs. Generics are irrelevant here; generics serve to link things together, they are not a solution for dynamic typing. Just the basic java typing system itself does that; e.g. Number n = foo(); lets you assign an Integer, a Double, a Float, or some custom number type you made in your own project to n, there is no need to bring generics into it. Generics is only useful if you want to write a method and you want to express for example: "Param 1 is some type, and param 2 is a list whose components are guaranteed to be the same type" - you want a generics term to show up in at least 2 places or it's useless.

At best you're looking a something like:

public interface CourseValue {}

public enum A implements CourseValue { A1, A2; }
public enum B implements CourseValue { B1, B2; }
public enum C implements CourseValue { B1, C2; }

public class Course {
  private List<CourseValue> values;
  public Course(CourseValue... courses) {
    values = List.copyOf(courses);
  }
}

...

new Course(Enums.A.A1, Enums.B.B2);

If you use java 17 you can 'seal' the interface (make it so that only explicitly listed types, e.g. A B and C, can implement it).

CodePudding user response:

In your third code block from the top, Java doesn't know where enums A, B, or C are. Since these are static enums within a class, you need to reference them using the class first.

Enums.A a1 = Enums.A.valueOf("a1");

As for the constructor of your Course class, you've went a little overboard on your implementation. You're trying to parameterize your Course object to a type of enum. As far as I can tell, you should only be concerned with the parameters in the constructor, not the method itself. You can pass in a list parameterized to the wildcard ? or parameterize it to accept an Object:

public Course(List<Object> enumsList) {
   // Do what you need to with your enum list.
}

See: how do you make an arraylist of different enums?

  • Related