Jawad Tariq
Jawad Tariq

Reputation: 355

How to convert multiple attributes of object into List<String> using java 8

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

Upvotes: 7

Views: 2300

Answers (1)

David P&#233;rez Cabrera
David P&#233;rez Cabrera

Reputation: 5068

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

Upvotes: 7

Related Questions