2

I have a list inside the list have many objects. every objects has selectedDates as list of String. now i want all the objects of selectedDates in a list. I tried but it is showing the following error. please help me with it. Error:

Type mismatch: cannot convert from List<List<String>> to List<String>

Code:

PlacesListExperienceCartItem expList = cartItemListRepo.save(experienceListCart);

List<String> selectedDateList = expList.getExperienceList()
                    .stream()
                    .map(PlacesExperienceCartConfirmRequest::getSelectedDates)
                    .collect(Collectors.toList());

Bean:

public class PlacesListExperienceCartItem {

    @Id
    String id;

    String customerId;

    String placeId;

    Double totalPrice;

    List<PlacesExperienceCartConfirmRequest> experienceList;

    private String bookingId;

    private String name;

    private String phone;

    private String email;

    private Location deliveryLocation;

    private List<String> selectedDates;


    public List<String> getSelectedDates() {
        return selectedDates;
    }

    public void setSelectedDates(List<String> selectedDates) {
        this.selectedDates = selectedDates;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPlaceId() {
        return placeId;
    }

    public void setPlaceId(String placeId) {
        this.placeId = placeId;
    }

    public Double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public List<PlacesExperienceCartConfirmRequest> getExperienceList() {
        return experienceList;
    }

    public void setExperienceList(List<PlacesExperienceCartConfirmRequest> experienceList) {
        this.experienceList = experienceList;
    }

    public String getBookingId() {
        return bookingId;
    }

    public void setBookingId(String bookingId) {
        this.bookingId = bookingId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Location getDeliveryLocation() {
        return deliveryLocation;
    }

    public void setDeliveryLocation(Location deliveryLocation) {
        this.deliveryLocation = deliveryLocation;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

}
Holger
  • 285,553
  • 42
  • 434
  • 765
Navaneethan Arun
  • 386
  • 3
  • 5
  • 17

1 Answers1

4

You need .flatMap instead of .map

List<String> selectedDateList = expList.getExperienceList()
                    .stream()
                    .flatMap(a -> a.getSelectedDates().stream())
                    .collect(Collectors.toList());

The .map method applies a function to each element of the stream, i.e. one object in -> one (different) object out

The .flatMap method turns every element of the stream into a new stream of elements (in your case the elements of the List), so one object in -> multiple objects out. All the elements of these new streams form the stream that is returned by .flatMap.

A more extensive explanation can be found here, as noted by Holger in the comments already.

Community
  • 1
  • 1
Robin Topper
  • 2,295
  • 1
  • 17
  • 25
  • 4
    If you add an explanation for the difference between `map()` and `flatMap()` I'll give you 10 internet points. – Kayaman Apr 28 '17 at 10:16