Last active
April 7, 2017 05:49
-
-
Save arinto/10023649 to your computer and use it in GitHub Desktop.
A target-typing example in Java 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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