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());
}
1
  • 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? Commented Feb 7, 2019 at 11:54

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed the first thing that came to my mind as well. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.