Skip to content

Instantly share code, notes, and snippets.

@arinto
Last active April 7, 2017 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arinto/10023649 to your computer and use it in GitHub Desktop.
Save arinto/10023649 to your computer and use it in GitHub Desktop.
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