Android and Stream Java 8

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 I configured the libim annimon: stream and backlamb, 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);
    
asked by anonymous 05.08.2017 / 00:20

1 answer

2

If the goal is to return an object only, I would swap the get and get get by:

.findFirst (). orElse (null)

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

    
05.08.2017 / 01:29