Skip to content

Instantly share code, notes, and snippets.

@arinto

arinto/TargetTyping.java

Last active Apr 7, 2017
Embed
What would you like to do?
A target-typing example in Java 8
public class TargetTyping {
public static void main(String[] args) throws IOException {
String value = System.getProperty("user.dir");
File f = new File(value);
//without target typing
System.out.println("Without target typing example, to find file that ends with xml");
for(File resFile: f.listFiles((File sf) -> sf.getName().endsWith(".xml"))) {
System.out.printf("%s%n", resFile.getAbsolutePath());
}
//simplified expression with target typing
System.out.println("With target typing example, to find file that ends with xml");
for(File resFile: f.listFiles(sf -> sf.getName().endsWith(".xml"))) {
System.out.printf("%s%n", resFile.getAbsolutePath());
}
//file filter, the instance is inferred as file
System.out.println("With file filter, to find file/directory that ends with \"src\"");
FileFilter src = theInstance -> theInstance.getName().endsWith("src");
for(File resFile: f.listFiles(src)) {
System.out.printf("%s%n", resFile.getAbsolutePath());
}
//directory filter, the instance is inferred as Path
System.out.println("With path filter, to find directory that ends with \"src\"");
DirectoryStream.Filter<Path> anotherFilter = theInstance -> theInstance.endsWith("src");
Files.newDirectoryStream(f.toPath(), anotherFilter).forEach(p -> System.out.println(p.toString()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.