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>>
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80

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())
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140