从List <string>转换为List <character> Java

时间:2015-09-19 06:22:51

标签: string list collections java-8 character

AFAIK,一些可能的转换是

List<String> stringList = integerList.stream().map(Object::toString)
                                    .collect(Collectors.toList());

List<Integer> integerList = stringList.stream()
        .map(Integer::valueOf).collect(Collectors.toList());

是否有像上面这样的转换从字符串列表转换为字符列表?

1 个答案:

答案 0 :(得分:6)

不确定

List<Character> result = 
    stringList.stream()
              .flatMapToInt(String::chars)
              .mapToObj(i -> Character.valueOf((char) i)
              .collect(Collectors.toList());

或只是

List<Character> result = 
    stringList.stream()
              .flatMapToInt(String::chars)
              .mapToObj(i -> (char) i)
              .collect(Collectors.toList());