Android and Stream Java 8

Asked

Viewed 46 times

0

Gentlemen I made a simple code to filter a list by passing the id and the goal would be to return an object. To make use of the stream configured the libs annimon:stream and retrolamba, I would like a critique of the code below, since it is my first contact with these functions of Java 8

return Stream.of(lista)
    .filter( e -> e.getId() == id)
    .map(e -> new Especialidade(e.getId(), e.getNome()))
    .collect(Collectors.toList())
    .get(0);

1 answer

2

If the goal is to return one object only, I would swap the Collect snippet and get it by:

.findFirst(). orelse(null)

And if the list is already specialties, the map is redundant.

  • Hi, Julio, hello, Julio! Yes the goal is to return an object, as I am sending an id to the filter so it should return a single object or null, thank you very much for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.