Java -> stream output into a List

snotnose

Ars Tribunus Militum
2,747
Subscriptor
Either I'm not grokking streams like I think I do or my cat snuck a stupid pill into breakfast. I'm trying to read a directory and put all .rle files into a List of some sort, doesn't matter what. I can print the files just fine, but I can't seem to make a list out of them:
Code:
// make a list of all .rle files and put them in a combobox.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GetRLEFiles {
    private void figgurItOut() {
        String root = "./patterns";
        List<String> patterns;
        boolean works = false;
        try {
            if(works) {
                Files.find(Paths.get(root), 999, (p, file) ->
                    file.isRegularFile()
                   && p.getFileName().toString().matches(".*\\.rle"))
                  .forEach(System.out::println);
           }
           else {
              //patterns = Files.find(Paths.get(root), 999, (p, file) ->
              //        file.isRegularFile()
              //        && p.getFileName().toString().matches(".*\\.rle"));
                    //.toArray();
                    //.toList();
          }
    } catch(IOException e) {
        System.err.println("IO error: " + e);
     }
   }
   public static void main(String[] args) {
        GetRLEFiles g = new GetRLEFiles();
        g.figgurItOut();
   }
}
Sorry for the formatting, cut and paste isn't working (points towards the cat sneaking me a stupid pill).
 

snotnose

Ars Tribunus Militum
2,747
Subscriptor
stream.collect(Collectors.toList()) ?
Doesn't compile. I can do a lot of stuff with toList() and friends, but as soon as I put "patterns = " in front of it I get a compile error.

Streams is one of those things I always think I understand until I try to use them. Then I get either errors I don't understand or results that aren't what I wanted.
 

Lt_Storm

Ars Praefectus
16,294
Subscriptor++
Got it.
Code:
    List<String> patterns = Files.find(Paths.get(root), 999, (path, file) ->
                    file.isRegularFile() &&
                    path.getFileName().toString().matches(".*\\.rle"))
                    .map(Path::toString)
                    .collect(Collectors.toList());

I now have an idea of how much I don't understand about lambdas and streams.
Not sure that the problem is an understanding of lambdas and streams so much as not paying enough attention to the difference between List<Path> and List<String>.