• Post Reply Bookmark Topic Watch Topic
  • New Topic

Alternatives to enum?  RSS feed

 
Preethi Vallam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a requirement in my application where i want to store some values in a hashmap which can be one of the 3 defined strings or one of the strings that are created at runtime based on the users input. I thought of using a enum for the 3 defined strings.. but for the strings that i construct at runtime based on user input, enum cant be used. Is there some good solution for this apart from having a string constants file that i construct at runtime?

In fact, is there any other alternative to enum at all?


Thanks in advance.
Preethi
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds as if you were trying to work out how to do something before you have told us what you are trying to do. Why do you need a Map? Why have you got some predetermined Strings and some created later?
If you do need a Map, what is wrong with a plain simple Map<String, Foo>?
 
Preethi Vallam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I am trying to store a 2d matrix. For explanation purposes, let me give an example:
1. There are set of strings - A ,B,C
2. The matrix is going to have values for [A,A],[A,B],[A,C],[B,A],[B,B],[B,C] and so on...
3. The values in the matrix can be -X,Y,A,B,C.

I could have used a simple array, but there is a possibility that more strings get added in the future... like D, E, F etc. In which case I do not want to manually resize my data structure. Hence I am using a map.

Now, my problem is that I want to make sure that only values X, Y,A,B,C are allowed as my hashmap values. But A,B,C is something thats based on user input, so cant use a enum. What do you suggest?

Thanks
Preethi
 
Preethi Vallam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To just be more clear:
In order to store my 2d matrix as an array, i created another class which holds the 2 strings of row and column members and am using this new structure as my key in hashmap. That is, my new structure has:


And my hashmap:
Hashmap <Structure,String>

Now... here if the values in the hashmap is stored just as String, then, there is a possibility that any value be stored. But I want to ensure the values stored are X,Y,A,B,C only.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An enum can only be used for a fixed set of constants that are known at compile time, so if the set of values is something that is based on user input, you can indeed not use an enum.

The reason for this is that it gives the compiler the possibility to check if you use the values correctly - since all the possible values are known at compile time, the compiler can check that you are not assigning any other, invalid value to your variables anywhere. Of course, if the possible values are not known at compile time, there is no way for the compiler to do this kind of check.

So, if the set of values is not fixed at compile time, you'll have to do something else.

To make sure that no value other than one of the allowed values is stored, you could make a class that contains the Map, and then let that class do a check before you put something in the Map. Then, don't give the rest of the program direct access to the Map, but let it work through an instance of your wrapping class. Something like this:

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be wrong with a List<String[]>?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!