C# Lambda for Java Lambda

Asked

Viewed 37 times

-1

I have this code in C#. A lambda expression that searches for files in a directory using regex and plays those files in a function by running a command line. All found files are added in "Task" to run in parallel. How would the corresponding of this expression in Java?

_ = await Task.Run(() =>
          {
              return Parallel.ForEach(GameFiles.Where(f => Regex.IsMatch(f, pattern: FileLocationInfo.DlgPattern)), item => tw.GetFile($"-e -t", item));
          });

Edit: "Gamefiles" contains the folder with several files from a game. "Regex" is used to select only the files I want. It has files with the same extension that I don’t care about. "tw.Getfile" is a method with the command-line app.

"Regex" selects the files in "Gamefiles", that are played one by one in the method "tw.Getfiles" and then executed in parallel.

The app is a few KB and the files have 4MB at most. It is more convenient to run in parallel.

  • does not seem to me to be a question of the lambda... but rather the appeal of the Paralell.ForEach. Present the code of how you are trying to play this chunk of code in Java.

  • In relation to Paralell.ForEach you can through Stream::parallelStream.

1 answer

0

In principle it will be any of the kind, assuming that the GameFiles is a list of filenames. You need to see the condition of Where that in Java is filter and mapping through the map. In the end I put everything on a list with the call to collect(Collectors.toList()).

gameFiles.parallelStream()
    .filter(f => ...)
    .map(item => ...)
    .collect(Collectors.toList())

For more about Parallel Streams in Java see this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.