我正在尝试从Map<String, GraphNode<Character>>对构造List<Pair<GraphNode<Character>, List<String>>>。但是我面临的问题是它给出了错误信息,说明类型不匹配:无法从Map<Object,Object>转换为Map<String,GraphNode<Character>>

我应该如何强制返回Map<String,GraphNode<Character>>

List<Pair<GraphNode<Character>, List<String>>> pairs = lines.stream().map(line -> {
            String[] split = line.split(":");
            List<String> dest = Lists
                    .newArrayList(Splitter.on(",").trimResults().omitEmptyStrings().splitToList(split[1]));
            GraphNode<Character> node = new GraphNode<>(split[0].toCharArray()[0]);
            return new javafx.util.Pair<GraphNode<Character>, List<String>>(node, dest);
        }).collect(Collectors.toList());
Map<String, GraphNode<Character>> graphMap = pairs.stream().map(pair -> pair.getKey())
                    .collect(Collectors.toMap(GraphNode::getData, pair -> pair));

最佳答案

由于您的班级具有Character类型(GraphNode<Character>),因此我认为GraphNode::getData也返回与Character相关的类型。

但是您将其分配给了String中的Map<String, GraphNode<Character>>,因此类型不匹配。

解决方法是将GraphNode::getData映射到String

10-04 14:00