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());
}
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
Correct Option: B. singletonList() returns the object as an immutable List. This is an easy way to convert a single object into a list.
//Assuming that your object is a valid List object, you can use: Collections. singletonList(object) -- Returns an immutable list containing only the specified object. //Similarly, you can change the method in the map to convert to the datatype you need.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With