0

I have 2 lists codeList and nameList both having String elements.

    codeList = ["1", "2", "3"];
    nameList = ["One", "Two", "Three"];

I want to combine these in such a way that the elements on same index come together in resultant String separated with |:

"1{One} | 2{Two} | 3{Three}"

Is there any way of directly getting the resultant list or String from the 2 list itself using Java 8

I am able to achieve this by using a Map but not list.

Andronicus
  • 23,098
  • 14
  • 38
  • 73
Akash Sharma
  • 106
  • 7

1 Answers1

4

You can use IntStream with indices of those lists:

IntStream.range(0, codeList.size())
    .mapToObj(i -> codeList.get(i) + "{" + nameList.get(i) + "}")
    .collect(Collectors.joining("|"));
Andronicus
  • 23,098
  • 14
  • 38
  • 73