I am new in functional programming in java 1.8. I have simple loop like the below code:
File folder = new File("./src/renamer/Newaudio");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
System.out.println("Here is file number: " + i);
}
I am going to change the upper for loop
to the functional format using the arrow function (like a lambda).
listOfFiles.map((file) = > {
System.out.println("Here is file number: " + i);
});
Unfortunately, it complains with:
Cannot invoke map((<no type> file) -> {}) on the array type File[]
Why i get this error? how i can resolve it?
I like to learn about Lambda, so i am not interested to the foreach
.forEach
instead of.map
. Note that you can useFiles.list(Paths.get("./src/renamer/Newaudio")).forEach(...)
to jump into the Stream API directly.i
in your lambda?File
class. It's outdated and should be laid to rest. UsePath
andFiles
.