Shozab
Shozab

Reputation: 47

Why to cast a list in another list

I have a method in Spring Data JPA which returns a type of list.

List<ModuleAccessEntity> findByNameNotIgnoreCase(String moduleName);

Now on ServicesImplimentation. This list is transferred to another list.

moduleAccessEntity=(List<ModuleAccessEntity>) moduleAccessRepository.findByNameNotIgnoreCase("self");

here moduleAccessEntity also is list of type.

List<ModuleAccessEntity> moduleAccessEntity ;

I don't understand why is there a need of putting list in two more lists when we are already getting a list. Can't we do this in easy way like.

moduleAccessEntity= moduleAccessRepository.findByNameNotIgnoreCase("self");

Upvotes: 1

Views: 80

Answers (2)

Ben R.
Ben R.

Reputation: 2153

This seems like a redundant cast, and can be removed. Some IDEs flag this to you as unnecessary.

Upvotes: 1

kAliert
kAliert

Reputation: 768

The list doesn´t get transferred, it gets castet to List<ModuleAccessEntity>. Maybe some earlier implementation has resturned a list without the type, so you had to cast the list. As long as i can see, you don´t need that cast, so change your code as follows:

moduleAccessEntity = moduleAccessRepository.findByNameNotIgnoreCase("self");

Upvotes: 1

Related Questions