I have a String list like below.
[
"Java",
"Kotlin",
"Python"
]
Is there any way (in both Java and Kotlin) to get the final output as below using String.join() or any other simple way instead of using a loop and adding numbers manually?
"1. Java\n2. Kotlin\n3. Python"
In Java, if you don't want to write explicitly loops, there isn't much else to use other than streams. Since we can't keep track of an index if we loop over the strings, we have to stream over the indices of the list instead:
I think that's not really a lot shorter than if you had done it with a loop.
In Kotlin on the other hand, there is
mapIndexedwhich is designed for exactly this kind of situation: