Legato
Legato

Reputation: 610

Are there advantages to using an enum where you could use a map and vice versa?

Say, for example, I want to make a cash register program. Ignoring, for the sake of being compact, that one wouldn't use floats for currency my first instinct is to use an enum for the denominations, something along the lines of :

private enum Currency {
        ONE_HUNDRED(100.00f),
              FIFTY( 50.00f),
             TWENTY( 20.00f),
                TEN( 10.00f),
               FIVE(  5.00f),
                TWO(  2.00f),
                ONE(  1.00f),
        HALF_DOLLAR(  0.50f),
            QUARTER(  0.25f),
               DIME(  0.10f),
             NICKEL(  0.05f),
              PENNY(  0.01f);

        private final float value;

        Currency(float value) {
            this.value = value;
        }

        public float getValue() {
            return this.value;
        }

        @Override
        public String toString() {
            return this.name().replace("_", " ");
        }
    }

But last I followed instinct, sans forethought, and did something similar for a Morse Code Converter, someone suggested that I use a map instead, explicitly a Bimap. I see the appeal of that collection in that particular scenario, but generally speaking I wanted to inquire if there were any reason to prefer one when the other could be used? If instead of the above code I did this:

Map<String, Float> currency = new LinkedHashMap<>();

    currency.put("One Hundred", 100.00f);
    currency.put("Fifty", 50.00f);
    currency.put("Twenty", 20.00f);
    currency.put("Ten", 10.00f);
    currency.put("Five", 5.00f);
    currency.put("Two", 2.00f);
    currency.put("One", 1.00f);
    currency.put("Half Dollar", 0.50f);
    currency.put("Quarter", 0.25f);
    currency.put("Dime", 0.10f);
    currency.put("Nickel", 0.05f);
    currency.put("Penny", 0.01f);

Would it be superior for any reason?

In cases like these were either could be utilized, are there any performance advantages to using one over another? Is one more preferable/conventional? More maintainable/adaptable?

Is there any rule of thumb I could use for when I should use one over the other?

Upvotes: 4

Views: 847

Answers (4)

gknicker
gknicker

Reputation: 5569

Would it be superior for any reason?

The Map design would be appropriate for dynamic data, whereas the enum design would be appropriate for fixed data.

In cases like these were either could be utilized, are there any performance advantages to using one over another?

Insignificant.

Is one more preferable/conventional?

Only when considering the specific problem to be solved.

More maintainable/adaptable?

Again, it depends on the problem you're trying to solve.

Is there any rule of thumb I could use for when I should use one over the other?

Whether you're working with a limited, non-varying dataset known at compile time.

Upvotes: 0

RutledgePaulV
RutledgePaulV

Reputation: 2606

Here are things I like to keep in mind:

Enums are best used (and in the languages I know of, may only be used) to define a known set of items ahead of time. This has a nice benefit of treating what really boils down to frequently used "data" as code in a very readable way.

In my opinion, any code that relies on frequently hardcoded strings, like you would need to use if implementing data like that in a map is more difficult to read and maintain. This leads to "magic strings", which is a no-no when avoidable.

It's not immediately clear what should exist in the map until you go check, and it's not clear if it's potentially being modified elsewhere. Consider, that if you got an enum value wrong, the code will not even compile. Get a string key wrong, and you might not notice until much later.

Regarding performance, I doubt there is a large difference between the two. Enums are treated largely the same as objects, I suppose the benefit comes from accessing the data as a field on the object rather than a hash lookup.

This article doesn't go in depth as I would like, but may be a good starting point: Memory Consumption of Java Data Types

It is quite common practice to use an enum as keys for a known map and that offers another way of associating data with a set of specific items (rather than setting them as fields on the enum). I believe this approach would be my preferred method since setting lots of fields on an enum makes them feel too much like a class rather than a method of referencing. This doesn't have the same problems as a normal map because since the keys must be enums you don't need to worry about any other keys "accidentally" being added to the map. It seems Java as a whole supports this approach as they provide the EnumMap class.

Upvotes: 1

Cody Anderson
Cody Anderson

Reputation: 124

I would use enum in this case it is more sensible and if this were something that were to be used by other people enum's have the associated values display for you if you are using pretty much any ide, where as if you are using a map neither the key or the value is readily available to you. There are other reasons but that was one that came to mind.

Upvotes: 0

Nikem
Nikem

Reputation: 5784

I would say that the main difference between your two pieces of code is that in case of enum you have fixed list of denominations which are "type-safe". While operating with strings and maps it is very easy to misspell some string, introducing bugs that are hard to spot.

Upvotes: 0

Related Questions