7

I am wondering if there is a way to combine multiple attributes from an object into a list of String. In My Case, I have an object with the name "debitCardVO" and I want it to convert from object to List

Here is my code Snippet:

for (DebitCardVO debitCardVO : debitCardVOList) {
    List<String> debitCardList   = debitCardVOList.stream()
            .map(DebitCardVO::getCardBranchCode,DebitCardVO::getAccountNo)
            .collect(Collectors.toList());
}
Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Jawad Tariq
  • 335
  • 1
  • 3
  • 14
  • 2
    What's the use case for that? Assuming all those attributes can be converted to strings what would you do with that? If it's for serialization why not use Json or some other string representation? – Thomas Feb 07 '19 at 11:54

1 Answers1

7

flatMap can help you flatMap vs map

List<String> debitCardList = debitCardVOList.stream()
                    .flatMap(d -> Stream.of(d.getCardBranchCode(),d.getAccountNo()))
                    .collect(Collectors.toList());

Other examples here

Naman
  • 27,789
  • 26
  • 218
  • 353
David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37