如何平面Map自定义创建的类?

6g8kf2rb  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(301)

我对java(特别是java-8风格)是新手,我正在尝试获取一个包含围绕其他坐标的坐标的列表。最好用一个例子来说明。
我定义了一个自定义类,它是一个坐标,如下所示:

public class Coord {
    public final int row;
    public final int col;

    public Coord(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public List<Coord> surroundingCoordinates() {
        return Arrays.asList(
                // Up & down
                new Coord(row + 1, col), new Coord(row - 1, col),
                // Left and right
                new Coord(row, col + 1), new Coord(row, col - 1),
                // Diagonals
                new Coord(row - 1, col - 1), new Coord(row - 1, col + 1),
                new Coord(row + 1, col - 1), new Coord(row + 1, col + 1)
        );
    }

    public static List<Coord> surround(Coord c) {
        return Arrays.asList(
                // Up & down
                new Coord(c.row + 1, c.col), new Coord(c.row - 1, c.col),
                // Left and right
                new Coord(c.row, c.col + 1), new Coord(c.row, c.col - 1),
                // Diagonals
                new Coord(c.row - 1, c.col - 1), new Coord(c.row - 1, c.col + 1),
                new Coord(c.row + 1, c.col - 1), new Coord(c.row + 1, c.col + 1)
        );
    }
}

我试图构建一个函数(静态方法),它遍历一组坐标,并生成一个包含所有周围坐标的扁平列表。换句话说,我想将一个函数Map到一个坐标列表,然后将结果展平,得到一个坐标列表。我的函数看起来像:

public static Stream<Coord> getSurroundingCoordinates(Coord[] coordinates) {
    // Turn the array of coordinates into a List
    List<Coord> coordsList = Arrays.asList(coordinates);
    // Trying to map and then flatten (fails)
    return coordsList.stream().flatMap(Coord::surroundingCoordinates);
}

我遇到ide编译错误(在coord::surroundingcoordinates处):

Required type: Function
Provided: <method reference>

所以我试着用一个静态方法来代替一个方法,比如:

public static Stream<Coord> getSurroundingCoordinates(Coord[] coordinates) {
    // Turn the array of coordinates into a List
    List<Coord> coordsList = Arrays.asList(coordinates);
    // Trying to map and then flatten (fails)
    return coordsList.stream().flatMap(c -> Coord.surround(c));
}

我在c->coord.surround(c)处遇到ide编译错误:

Required type: List<Coord>
Provided: Stream <Object>

我想要的最终结果是一个平面坐标集合

y4ekin9u

y4ekin9u1#

你只是语法有点错误,我猜:

public static Stream<Coord> getSurroundingCoordinates(Coord[] coordinates) {
    return Arrays.stream(coordinates).flatMap(c -> c.surroundingCoordinates().stream());
}
vyu0f0g1

vyu0f0g12#

在这种情况下,甚至出于性能方面的考虑,让环绕方法返回流而不是列表也是更好的选择。就这样做:

class Coord {

public static Stream<Coord> surround(Coord c) {
    return Stream.of(
            new Coord(c.row + 1, c.col), new Coord(c.row - 1, c.col),
            new Coord(c.row, c.col + 1), new Coord(c.row, c.col - 1),
            new Coord(c.row - 1, c.col - 1), new Coord(c.row - 1, c.col + 1),
            new Coord(c.row + 1, c.col - 1), new Coord(c.row + 1, c.col + 1)
    );
}

public static Stream<Coord> getSurroundingCoordinates(Coord[] coordinates) {
    List<Coord> coordsList = Arrays.asList(coordinates);
    return coordsList.stream().flatMap(Coord::surround);
}}

如果您想稍后从流中获取列表,则比每次迭代时将列表转换为流更容易:

surround(c).collect(Collectors.toList());

相关问题