Java 8/9: Can a character in a String be mapped to its indices (using streams)?

后端 未结 4 1685
抹茶落季
抹茶落季 2021-02-12 14:46

Given a String s and a char c, I\'m curious if there exists some method of producing a List list from s (where

4条回答
  •  醉酒成梦
    2021-02-12 15:27

    Can be done with IntStream

    public static List getIndexList(String s, char c) {
        return IntStream.range(0, s.length())
                        .filter(index -> s.charAt(index) == c)
                        .boxed()
                        .collect(Collectors.toList());
    }
    

提交回复
热议问题