Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding enum type to a list

Tags:

java

enums

If I need to add an enum attribute to a list, how do I declare the list? Let us say the enum class is:

public enum Country{ USA, CANADA; }

I want to do:

List<String> l = new ArrayList<>();
l.add(Country.USA);

What needs to be used instead of List<String>?

like image 438
Victor Avatar asked Aug 07 '11 00:08

Victor


2 Answers

Should be:

List<Country> l = new ArrayList<Country>();
l.add(Country.USA); // That one's for you Code Monkey :)
like image 195
MByD Avatar answered Sep 22 '22 09:09

MByD


If you want the string type use this:

l.add(Country.USA.name());

otherwise the answer of MByD

like image 32
timaschew Avatar answered Sep 21 '22 09:09

timaschew