Java 8/9: Can a character in a String be mapped to its indices (using streams)?
Given a String s and a char c , I'm curious if there exists some method of producing a List<Integer> list from s (where the elements within list represent the indices of c within s ). A close, but incorrect approach would be: public static List<Integer> getIndexList(String s, char c) { return s.chars() .mapToObj(i -> (char) i) .filter(ch -> ch == c) .map(s::indexOf) // Will obviously return the first index every time. .collect(Collectors.toList()); } The following inputs should yield the following output: getIndexList("Hello world!", 'l') -> [2, 3, 9] Can be done with IntStream public static