收集字符集流

我有这个字符集流:

Stream<Set<Character>> characterSet

现在,我想将此流转换为简单的字符集,然后执行以下操作:

Set<Character> newSet = characterSet.collect(Collectors.toSet());

但是,我收到此错误:

Incompatible types. Required Set<Character> but 'collect' was inferred to R: no instance(s) of type variable(s) exist so that Set<Character> conforms to Character inference variable T has incompatible bounds: equality constraints: Character lower bounds: Set<Character>.

我该如何解决?谢谢!

评论
WOW
WOW

如下进行:

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Set<Character> newSet = Stream
                .of(Set.of(Character.valueOf('A'), Character.valueOf('B')),
                        Set.of(Character.valueOf('C'), Character.valueOf('D')))
                .flatMap(s -> s.stream()).collect(Collectors.toSet());
        System.out.println(newSet);
    }
}

输出:

[A, B, C, D]
点赞
评论