I'm trying to retrieve an object(User) using stream and I can't. Each user has a unique code, and what I want is to retrieve the user with the code "such".
If I cast, when I run the App, it gives me a classCastException.
public Usuario findUser(List<?extends Usuario> usuarios, String codigo)
{
return (Usuario)usuarios.stream()
.filter(usuario -> usuario.getCodigo().equals(codigo));
}
I imagine that it will not be necessary to cast and that I was missing code, thank you very much in advance for the little help.
EDITED:
This is what I have done and it worked for me:
public Alumno findAlumno(List<Alumno> alumnitos, String codigo) {
return alumnitos.stream()
.filter(alumno -> alumno.getCodigo().equals(codigo))
.findFirst().orElse(null);
}
Below a colleague has a solution that also works and is very very similar to this one, almost the same, there are subtle differences. But I want to record what I have done and it has worked for me in case anyone is interested.
I consider that his solution is not the same as mine, because, among other things, he uses an "optional" and makes a "cast", I did not need it
Thank you very much