I also thought that there should be a map(predicate, functionIfTrue, functionIfFalse) method map(predicate, functionIfTrue, functionIfFalse) , but it is not. Instead, use a three-dimensional map on the map:
final int[] idx = {-1}; // trick to get around "effectively final" String result = Arrays.stream(input.split(" ")) .map(s -> MIN_IDX <= ++idx[0] && idx[0] <= MAX_IDX ? s.toUpperCase() : s) .collect(joining(" "));
This uses the trick to get the iteration index using a single array of elements, which itself is final, but whose contents are modified.
Some test codes:
final String input = "one two three four five six seven eight"; final int MIN_IDX = 2; final int MAX_IDX = 5; final int[] idx = {-1}; String result = Arrays.stream(input.split(" ")) .map(s -> MIN_IDX <= ++idx[0] && idx[0] <= MAX_IDX ? s.toUpperCase() : s) .collect(joining(" ")); System.out.println(result);
Output:
one two THREE FOUR FIVE SIX seven eight
source share