0

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.

Belphegor21
  • 454
  • 1
  • 5
  • 24
  • maybe it could work with reflection. other way would be to create a custom `toString` method 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 change – XtremeBaumer Sep 29 '17 at 06:27
  • What exactly are you trying to achieve? This does not seem to be a very clean design. – daniu Sep 29 '17 at 06:32
  • I need to convert the loan object to a list which is used later for various purpose. To do that i convert the object using the `getListFromLoan` which 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 – Belphegor21 Sep 29 '17 at 06:36

3 Answers3

2
Object someObject = getItSomehow();
for (Field field : someObject.getClass().getDeclaredFields()) {
    field.setAccessible(true); // You might want to set modifier to public first.
    Object value = field.get(someObject); 
    if (value != null) {
        System.out.println(field.getName() + "=" + value);
    }
}
Kasup Sri
  • 159
  • 6
2

A lambda function is just a function. Could you do what you need with a regular method?

If you want to achieve what you say (a method that, no matter how many attributes your class has, adds them all to a list) you'll need to either do it manually and update it every time you add/remove an attribute or use reflection

Something like this:

Loan loan = ...
List<String> loanAsList = new ArrayList<>();
for (Field f : loan.getClass().getDeclaredFields()) {
    field.setAccessible(true);
    Object value = field.get(loan); 
    loanAsList.add(value.toString());
}
Alberto S.
  • 7,409
  • 6
  • 27
  • 46
0

Lambda Expressions don't do magic in Java 8(well they do but not what you are expecting if I understood you right).

Look at lambda expression as an alternate to anonymous classes, with the advantage that you don't need to wrap it in a class for it implement a function(method in pre java 8 terms). Look up "Behaviour Parameterisation" for a better understanding.

Saying that you can use "Function" functional interface provided by jdk. Represents a function that accepts one argument and produces a result. So you can write a method

public List<String> getListOfAttributes(Loan loan, Function<Loan, List<String>> myFunction) {
return myFunction.apply(loan);
}

and then call this method

getListOfAttributes(loan, (loan) -> {
    List<String> loanAsList = new ArrayList();
    loanAsList.add(loan.getCustomerId());
    loanAsList.add(Integer.toString(loan.getTenure());
    loanAsList.add(Double.toString(loan.getAmount());
    return loanAsList;
}

so because of "behaviour parameterisation" you can pass in different functionalities to get the different List.

mrpw
  • 36
  • 2