Conteúdo do Curso
Java Data Structures
2. Additional Data Structures
Java Data Structures
enum
Let's move on to another quite interesting and, most importantly, distinctive data structure - enum
.
enum
(enumeration) is a special data type that provides a set of constants (enumerations).
Note
In case you've forgotten, a constant is an unchangeable value. It remains constant.
Let's take a look at creating an enum
. Creating an enum
differs significantly from other data structures, as it is done at the class level:
DaysOfTheWeek.java
Enum is needed so that we can easily refer to the constant. Since it's a list of constants, we access it in the following way:
main.java
Here, we simply access the enum
constant to understand which day it is today. Instead of creating many different constants, we just store an array of constants in the enum
. This is convenient and very practical. For example, in one of the previous challenges, we could have stored department names in an enum
and used the constant as a key.
Let's look at an example:
main.java
By the way, we can also retrieve all constant values from an enum as an array using the values()
method.
Let's take a look at an example:
main.java
We can also declare fields and methods in enums.
Let's consider an example of a traffic light:
main.java
In enums, methods work slightly differently. Since they are constants, they cannot be modified. Therefore, we add fields in the constructor of this enum
, after which we define the getAction()
method that immediately outputs the action to be taken for a specific traffic light. As you can see, we specify this action directly in the enum when enumerating constants.
Let's look at an example of calling such a method:
main.java
As you can see, with the help of the getAction()
method, we immediately obtained the action to be taken for a specific traffic light. You can change RED
to any other color and see what happens.
Methods and fields in enums are not used as often, but this functionality is present, so it's good to be aware of it. You can read more about enums in the Official Java documentation.
Let's summarize a bit:
Advantages of Enum in Java:
- Improved readability: Replacing numerical constants and string literals with named values makes the code more readable;
- Type safety: The compiler ensures type safety when working with enums, preventing type-related errors;
- Limited set of values: Enums provide a fixed set of values, making the code more predictable;
- Ability to add methods and fields: Enums can contain methods and fields, making them more powerful compared to simple enumerations.
Tudo estava claro?
Conteúdo do Curso
Java Data Structures
2. Additional Data Structures
Java Data Structures
enum
Let's move on to another quite interesting and, most importantly, distinctive data structure - enum
.
enum
(enumeration) is a special data type that provides a set of constants (enumerations).
Note
In case you've forgotten, a constant is an unchangeable value. It remains constant.
Let's take a look at creating an enum
. Creating an enum
differs significantly from other data structures, as it is done at the class level:
DaysOfTheWeek.java
Enum is needed so that we can easily refer to the constant. Since it's a list of constants, we access it in the following way:
main.java
Here, we simply access the enum
constant to understand which day it is today. Instead of creating many different constants, we just store an array of constants in the enum
. This is convenient and very practical. For example, in one of the previous challenges, we could have stored department names in an enum
and used the constant as a key.
Let's look at an example:
main.java
By the way, we can also retrieve all constant values from an enum as an array using the values()
method.
Let's take a look at an example:
main.java
We can also declare fields and methods in enums.
Let's consider an example of a traffic light:
main.java
In enums, methods work slightly differently. Since they are constants, they cannot be modified. Therefore, we add fields in the constructor of this enum
, after which we define the getAction()
method that immediately outputs the action to be taken for a specific traffic light. As you can see, we specify this action directly in the enum when enumerating constants.
Let's look at an example of calling such a method:
main.java
As you can see, with the help of the getAction()
method, we immediately obtained the action to be taken for a specific traffic light. You can change RED
to any other color and see what happens.
Methods and fields in enums are not used as often, but this functionality is present, so it's good to be aware of it. You can read more about enums in the Official Java documentation.
Let's summarize a bit:
Advantages of Enum in Java:
- Improved readability: Replacing numerical constants and string literals with named values makes the code more readable;
- Type safety: The compiler ensures type safety when working with enums, preventing type-related errors;
- Limited set of values: Enums provide a fixed set of values, making the code more predictable;
- Ability to add methods and fields: Enums can contain methods and fields, making them more powerful compared to simple enumerations.
Tudo estava claro?