I'm trying to map below list :
mappedCustomers = customers.stream().map(e ->e.setLastName("e")).collect(Collectors.toList());
Customer Class
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue
Long id;
String firstName;
String lastName;
public Long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
But I'm getting this error:
no instance(s) of type variable(s) R exist so that void conforms to R
Map function from the Stream API, is expecting a function that return a value.
<R> Stream<R> map(Function<? super T,? extends R> mapper)
However setters return value is void (no return value at all), therefore your code does not comply with the map method signature, if you want to iterate over a collection and execute an operation you could use forEach
void forEach(Consumer<? super T> action)
No need to do the collection as you initial collection will be the same but it will contain your modification.
customers.stream().forEach(x -> x.setLastName("e"))