'Java How to know if arraylist contains value in property of object [duplicate]
I want to find if the property numb in the ArrayList grids contains a certain value or not. Then I want to get the index of the entry that contains that value.
public class Grid {
public int numb;
public GridSeg(int numb) {
this.numb = numb;
}
}
public ArrayList<Grid> grids = new ArrayList<Grid>();
for (int i = 0; i < 6; i++) {
Grid grid = new Grid(i);
grids.add(grid);
}
/pseudo code since I don't know how it is done
if (grids.numb.contains(12)) {
if (grid.numb == 12) //get grid index
}
Solution 1:[1]
You can achieve it with streams
ArrayList<Grid> grids = new ArrayList<>();
Grid grid = grids.stream().filter(l -> l.numb == 12).findFirst().orElse(null);
int index = grids.indexOf(grid);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zfChaos |
