java中Stream Collectors.toList()中的不兼容类型

时间:2019-07-17 02:20:43

标签: java graph

我正在实现图形表示。

Map<V, List<E<V>>> g = new HashMap<>();

Graph类中的一种方法是

List<E<V>> getAllEdges() {
    List<E<V>> allEdges = new ArrayList<>();

    for(Map.Entry<V, List<E<V>>> entry: g.entrySet()) {
        allEdges.addAll(entry.getValue());
    }

    return allEdges;
}

但是我想使用

使其更短
List<E<V>> getAllEdges() {
    return  g.values().stream().collect(Collectors.toList());
}

但是我遇到了这样的错误

enter image description here

有没有办法使用流?

1 个答案:

答案 0 :(得分:6)

由于您的值已经输入为joining %>% group_split(source) %>% map_dfr(left_join, x = joined, by = 'key') ,因此,如果要构建joined <- structure(list(key = structure(1:4, .Label = c("A", "B", "C", "D"), class = "factor")), class = "data.frame", row.names = c(NA, -4L)) joining <- structure(list(key = structure(c(1L, 2L, 2L, 3L), .Label = c("A", "B", "C"), class = "factor"), source = structure(c(1L, 1L, 2L, 2L), .Label = c("a", "b"), class = "factor"), value = c(1L, 1L, 2L, 2L)), class = "data.frame", row.names = c(NA, -4L)) ,则使用List<E<V>是合适的。

要解决此问题,请使用.collect(Collectors.toList())展平2D列表:

List<List<E<V>>