Writing a for loop to build a String using Streams I have the following code which works. Been trying to write it in Java 8 streams to make it more readable but unable to get my head around it. Looking for any feedback to make it more readable.
Thanks. 

// Expected output like: 1~CODE1|2~CODE2|3~CODE3|4~EX|5~CODE5
private String getFormattedValue(List&lt;Content&gt; list) {
    final String tilde = "~";
    final String pipe = "|";
    StringBuilder codes = new StringBuilder();

    for (int i = 0; i &lt; list.size(); i++) {
        String code = list.get(i).getCode();
        if (StringUtils.isNotBlank(code) &amp;&amp; !"null".equals(code)) {
            code = "example".equals(code) ? "EX" : code.toUpperCase();
            codes.append(i + 1).append(tilde).append(code);
            codes.append(pipe);
        }
    }
    if (codes.toString().endsWith("|")) {
        codes.deleteCharAt(codes.length() - 1);
    }
    return codes.toString();
}
One issue is that you cannot consume a stream with index. You can more or less have the same result with a IntStream bounded to your list's size.

Then you just have map each int to a code with the same logic. And, finally, collect them (This is the only part where the Stream will shine).

String string = IntStream.range(0, codes.size())
            .mapToObj(i -&gt; {
                String code = codes.get(i);
                code = "example".equals(code) ? "EX":code.toUpperCase();
                return String.format("%1$d~%2$s", i, code);
            })
            .collect(Collectors.joining("|"));