I have a section of code that used to utilize Optional<Department>, but due to some errors I worked out I am now converting it to List<Department>. Obviously this means I now have to change the return types and other method calls. Here are my questions:
I changed my returns to "new LinkedList<>()" (indicated in the code below) but is that correct?
There is a red error under ".isPresent" (indicated in the code below) with error message "The method isPresent() is undefined for the type List<Department>". What should I be changing that to?
Below is the updated code with comments indicating where errors are now occurring. Any help and explanations would be GREATLY appreciated!
public List<Department> delete(String department_ID) {
if ((department_ID == null) || (department_ID.isEmpty())) {
return new LinkedList<>(); //<<<<<<<<<<<<<<< used to be "return Optional.empty();"
}
List<Department> existing = get(department_ID);
if (existing.isPresent()) { //<<<<<<<<<<< red error under ".isPresent()"
String sql = "DELETE employee.*, department.* " "FROM employee, department "
"WHERE employee.department_ID = :department_ID AND department.department_ID = :department_ID;";
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("department_ID", department_ID);
int rows = jdbcTemplate.update(sql, parameters);
if (rows > 0) {
return existing;
}
}
return new LinkedList<>(); //<<<<<<<<<<<<<<< used to be "return Optional.empty();"
}
- I changed my returns to "new LinkedList<>()" (indicated in the code below) but is that correct?
- I have googled the error message for my ".isPresent" error and cant find any explanations that fit
CodePudding user response:
Instead of returning new LinkedList<>()
you could return List.emptyList()
.
isPresent()
is a method of Optional, but you assign the outcome of method get(department_ID)
to an instance of List. You can check the List using
if(!(existing == null || existing.isEmpty())) {
CodePudding user response:
You said:
red error under ".isPresent" (indicated in the code below) with error message "The method isPresent() is undefined for the type List".
The variable existing
holds a reference to a List
object. If you look at the Javadoc for List
, you find no method named isPresent
. So of course trying to call a non-existent method generates an error from the compiler.
That isPresent
method was from the Optional
class. The method checks to see if the optional holds a payload or if the optional is empty.
You seem to be switching to a style where you always expect a List
, even if the list is empty (no elements).
If you want to be defensive, you can check that the list object exists.
if ( Objects.nonNull( existing ) ) { … }
But you can omit that null check if you are confident that such a condition cannot exist.
You may want to check if the list is empty, to avoid a needless call to your database. If so, change that code to:
if ( ! list.isEmpty() ) // Perform database work only if list has some elements.
You have other issues. Among them:
- Generally the convention in Java is to avoid underscores in names. And generally best to avoid ALL-CAPS. So
departmentId
, notdepartment_ID
. - When returning lists, generally best to return an unmodifiable list. If the calling programmer needs a modifiable list, they can easily make from the returned unmodifiable list.
- To get an unmodifiable list, use
List.of
rather thannew LinkedList<>()
. - I cannot understand why your
delete
method would return a list. Your code there mystifies me.