Consider this piece of code:
private static ArrayList<Level> levels = new ArrayList<>();
static {
try (Stream<Path> paths = Files.walk(Paths.get("levels"))) {
paths
.filter(Files::isRegularFile)
.forEach(levels.add(new Level(file))); //CAN'T DO!!
} catch (IOException e) {
e.printStackTrace();
}
}
I think the code pretty much says what I'm trying to do. I found this piece of code somewhere and I tried to apply it, to create Level objects from folder with files such as level1.txt, level2.txt, etc. The Level class takes a File as argument.
The forEach method gives out a SyntaxError.
- What are the "::" in the
filtermethod? - Why is the path followed by a new line and 2 methods? I've never seen such a thing before.
- How can I make this work properly?
paths.filter(Files::isRegularFile).forEach(levels.add(new Level(file)));which is Stream (chaining method here)(file) -> levels.add(new Level(file))into the forEach instead of an expression