Posts

Showing posts with the label java 8

Java: How to work with streams

Filter a list and process each element Filter a list and collect the elements Transform a list Reduce a list

Java : Functional interfaces and Lamda expression

I'll show you how you can check a condition and return a boolean value. The java API includes 3 functional interfaces commonly used: Predicate<T>  Consumer<T> Function<T, R> The Predicate tests the T object and returns a boolean value. The Consumer accepts an operation on T but does not return a value. The Function performs an operation on T and returns an R object. The static method below uses the Predicate interface to specify the condition: You can use this method like this: Thanks

Java : How to work with file and directory

The java.nio.file package (from Java 7) has improved the way we work with the file system. Create a directory For creating a new directory, we get a Path object for the provided path. Then, we check if the directory exists. If not, we call the static createDirectories() method of the Files class. Create a File For creating a file, we follow basically the same process. But we get a Path object that refers to the directory and file. If the file doesn't exist, we call createFile() method of the Files class. Iterate through Files in a directory By using the static newDirectoryStream() method of the Files class, you can list the files in a directory this way: