You wanna perform a search on the List and return the tile that matches the coordinates, you probably wanna change the Signature of the method to take in the List you wanna perform the search on and return the list of matches, and never return a null from a method that returns a collection, always return an empty collection instead.
public List<Tile> getTileAtPos(int xCoor, int yCoor, List<Tile> tiles) {
return tiles.stream()
.filter(t -> t.getXCoor() == xCoor && t.getYCoor() == yCoor)
.collect(Collectors.toList());
}
@Getter
class Tile {
private int xCoor;
private int yCoor;
}
If only one Tile can match the condition of being at x and y then the method changes to:
public Tile getTileAtPos(int xCoor, int yCoor, List<Tile> tiles) {
return tiles.stream()
.filter(t -> t.getXCoor() == xCoor && t.getYCoor() == yCoor)
.findFirst().orElse(null);
}
in this case it returns null if the tile is not found.
But I would prefere to have the method return an Optional instead, so the user of the method knows that he's not always guaranteed to have a result and mitigate the risk of nullpointer:
public Optional<Tile> getTileAtPos(int xCoor, int yCoor, List<Tile> tiles) {
return tiles.stream()
.filter(t -> t.getXCoor() == xCoor && t.getYCoor() == yCoor)
.findFirst();
}
tiles.get(yCoor * width + xCoor)ortiles.get(xCoor * height + yCoor), depending on how you define your grid.Tilerather thanArraylList<Tile>?