I have the following code :
userService.getUserWithAuthorityByLogin(principal.getName())
.map(user->userRepository.findAllByClient(user.getClient().getId()));
The method findAllByClient return a List<User>
type.
the map method returns a Optional<Object>
In my case it could (optional) return an Optional<List<User>>
.
The idea I want to code is to use a stream on this optional list.
I'd like to chain my calls. Code in java 8 style is good looking and fewer lines of code.
I need to process this List<User>
if it really exist (present). How can I chain my calls with java8 api : I wish to mix Optional and Stream.
Thanks.
EDIT :
I continue a bit further :
userService.getUserWithAuthorityByLogin(principal.getName())
.map(user->userRepository.findAllByClient(user.getClient().getId()).stream().forEach(user->
{
managedUserDTOs.add(new ManagedUserDTO(user));
}));
You've got the whole process. But this doesn't compile on instruction
managedUserDTOs.add(new ManagedUserDTO(user))
I want to fill the list and return it at final.
EDIT2 :
The whole wrapping method :
/**
* GET /users -> get all users.
*/
@RequestMapping(value = "/user-liste",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@Transactional(readOnly = true)
@Secured({AuthoritiesConstants.TC_ADMIN})
public ResponseEntity<List<ManagedUserDTO>> getUserListe(Principal principal)
throws URISyntaxException {
return new ResponseEntity<>(managedUserDTOs, HttpStatus.OK);
}