0

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.

  1. What are the "::" in the filter method?
  2. Why is the path followed by a new line and 2 methods? I've never seen such a thing before.
  3. How can I make this work properly?
4
  • 1
    "This code does not work." Please supply an adequate error description, including a full copy of the error and edit it into the question. Commented Apr 26, 2020 at 21:19
  • 1
    1 may google it (stackoverflow.com/questions/20001427/…). 2 just return line, same as paths.filter(Files::isRegularFile).forEach(levels.add(new Level(file))); which is Stream (chaining method here) Commented Apr 26, 2020 at 21:20
  • You should put a lambda like (file) -> levels.add(new Level(file)) into the forEach instead of an expression Commented Apr 29, 2020 at 14:43
  • I was expecting the complete error message, not just syntax error Commented Jul 21 at 14:27

1 Answer 1

1
  1. The following explains well what is :: (double colon) operator since Java 8

  2. Code can be run written on several lines, but this is same as the following. Your calling the method filter then calling forEach on the object returned by filter

    paths.filter(Files::isRegularFile).forEach(levels.add(new Level(file)));   
    
  3. Get it work, you need to define file variable this is done with lambda functions

    paths.filter(Files::isRegularFile).forEach(file -> levels.add(new Level(file)));   
    

NOT POSSIBLE because walks() throws IOException

In case you don't need the try/catch you can use Collectors to directly build the list

private static ArrayList<Level> levels = Files.walk(Paths.get("levels"))
                                              .filter(Files::isRegularFile)
                                              .map(file -> new Level(file))
                                              .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

5 Comments

The code does not work. I get SyntaxError in levels.add(new Level(file).
@DanielOscar I got none; So give the exact detail of the error and the constructor of Level class
The Level constructor just takes a File objects as parameter.
IDE suggest I change constructor of Level to receive a Path object instead
@DanielOscar walk gives out instances of Path not File, so you may adapt, use Path or get the file behind the path

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.