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

后端 未结 4 1681
抹茶落季
抹茶落季 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:35

    An alternate in Java9 could be to make use of the iterate(int seed, IntPredicate hasNext,IntUnaryOperator next) as follows:-

    private static List getIndexList(String word, char c) {
      return IntStream
              .iterate(word.indexOf(c), index -> index >= 0, index -> word.indexOf(c, index + 1))
              .boxed()
              .collect(Collectors.toList());
    }
    

提交回复
热议问题