0

I have created a function to transform the elements of a list:

private List<Hostel> build(List<Hotel> hotels) {
         return hotels.stream().map(h -> convert(h)).collect(toList());
    }

but I have a compilation error:

required type: List<Hostel>
Provided: List<List<Hostel>>
YCF_L
  • 51,266
  • 13
  • 85
  • 129
Sandro Rey
  • 1,473
  • 10
  • 24
  • 63

1 Answers1

2

From your error it seems convert(h) return a List<Hostel>, for that when you use a map, and collect the result is List<List<Hostel>>, to get List<Hostel>, you have to use flatMap instead of map, like this:

.flatMap(h -> convert(h).stream())
YCF_L
  • 51,266
  • 13
  • 85
  • 129