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;
    }

}
4
  • 3
    List<String> selectedDateList = expList.getExperienceList() .stream() .flatMap(e -> getSelectedDates().stream()) .collect(Collectors.toList()); Commented Apr 28, 2017 at 10:07
  • thanks man. can you explain me? why we need flatMap here? Commented Apr 28, 2017 at 10:17
  • 3
    See What's the difference between map and flatMap methods in Java 8? Commented Apr 28, 2017 at 10:19
  • Thanks man. @Holger Commented Apr 28, 2017 at 10:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you add an explanation for the difference between map() and flatMap() I'll give you 10 internet points.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.