I'm new to java lambda expression so i don't know exactly if what i'm asking is possible. If not possible please suggest a better way if any.
I have an class Object such as:
class Loan {
private String customerId;
private Integer tenure;
private Double amount;
...
}
I need to convert this object into a list of string. The way I'm doing it right now is:
List<String> loanAsList = getListFromLoan(loan);
public List<String> getListFromLoan(Loan loan) {
List<String> loanAsList = new ArrayList<>();
loanAsList.add(loan.getCustomerId());
loanAsList.add(Integer.toString(loan.getTenure());
loanAsList.add(Double.toString(loan.getAmount());
}
Can this be done using a lambda expression?
Loan has many more fields I have only shown a few. I want an expression in which no matter the number of field I could get a List.
toStringmethod for your object and then you have to split it correctly. this would be a kinda generic way since you only have to change the tostring method if any fields changegetListFromLoanwhich is very rigid. If later one or more fields are added to the Loan class this function needs to be changed. I don't want to do that. I want a function which can convert to a list irrespective of the number of member fields