Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of using Enums over Collections

Tags:

java

enums

I'm trying to check if a users account type matches one of several Strings.

There's debate in the office as to whether this should be represented as an enum with each entry containing a different string, or as a Set of Strings. Whilst the Set may be more efficient, an enum may be stylistically superior as it is clearer it is being used for logic flow.

What are the advantages of these two approaches?

like image 972
Finlay Smith Avatar asked Jul 20 '15 10:07

Finlay Smith


People also ask

What is the advantage of using enum?

The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future. Makes code easier to read, which means it is less likely that errors will creep into it.

What is the advantage of enum in Java?

You can declare a function argument, return type, class member or local variable to be a particular Enum type and the compiler will enforce type safety; Enums are basically classes. They can implement interfaces, have behaviour and so on.

Why enums are better than constants?

The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Why enums are better than strings?

I would consider Enums to be a better approach than Strings. They are type safe and comparing them is faster than comparing Strings. Show activity on this post. If your set of parameters is limited and known at compile time, use enum .


3 Answers

Indeed, a Set<String> is more efficient in terms of performance when searching. However, I wouldn't expect that you have thousands of account types, but several, so you won't actually feel the difference when searching. There's one problem with this approach, though - you will be able to add any String to the Set, which is brittle.

My personal prefer would be to use an enum, especially if you don't expect that more account types will be introduced. And if you have a Set<AccountType> you'll be restricted with the values you can add (i.e. you will be able to add only account types, but not anything, like the approach with a Set<String>). The problem with this approach is the Open/Closed Principle - consider you have a switch statement over a AccountType variable with all the corresponding cases. Then, if you introduce a new AccountType constant, you must change the switch statement (with adding a new case), which breaks the "Open/Closed principle". In this case the neatest design would be to have an abstract class/interface, called AccountType which has all the specific account types as sub-classes.

So, there are several approaches you can follow, but before picking one, you should try answer yourselves the question of "How are we going to use it?"

like image 168
Konstantin Yovkov Avatar answered Oct 23 '22 10:10

Konstantin Yovkov


An enum would be better since account types (typically) do not change dynamically. Furthermore, using an enum makes the types more precise - e.g. there's no way to mix up "Hello, World!" with an account type.

like image 13
ReyCharles Avatar answered Oct 23 '22 10:10

ReyCharles


Enums are great because you get compile time checking. Invalid values simply won't compile so it 'fails fast'.

A collection of strings is great when you want to add another option without compiling/releasing a new version of your application. If, for instance, the valid options were configured in a database table.

like image 6
lance-java Avatar answered Oct 23 '22 10:10

lance-java