Monday, September 04, 2017

WatchService to monitor Directories for changes

There a many scenarios where you would like to monitor a directory for new files, or changes to existing files or deleted files. Some of the use cases are
  • Files will be dropped into a directory by a third party which have to be processed by your application
  • A file editor which monitors the directory to make sure the files currently being edited are not modified by another concurrent user.
  • You want to start a build and deploy code whenever a source file is changed (In an enterprise application, this is usually done in a source control in conjunction with a tool like Jenkins).
Starting Java 7, the new java.nio.file.WatchService provides a scalable solution to monitor directory for any changes. In this post, we will go over the features of the WatchService service and a simple example for how to monitor a directory for changes and print the name of the file that was affected.

Friday, September 01, 2017

Directory Listing in Java

This post provides a examples of a few ways to list contents of a directory in Java. A couple of examples of how to recursively traverse a directory are also provided. Finally we will see the use of Files.walk() and Files.find() introduced in Java 8, which makes directory traversal even more simple.

Thursday, August 31, 2017

Java 8 Stream collect()

In Java 8 Streams, the collect() method is a terminal operation, which can be used collect elements of a stream into a list or group them into a Map etc. collect method takes java.util.stream.Collector as parameter. While you can create your own collector by implementing the java.util.stream.Collector interface, the more common way to instantiate a Collector is through the use of Collectors class. The following examples demonstrate a few ways to use the collect() method with different types of Collectors.

Wednesday, August 30, 2017

Java 8 Streams flatMap()

In Java 8 Stream API, flatMap() method is an intermediate functions for streams, in that, it will produce a final result but used to apply transformations on the elements of a Stream. It is possible to to chain multiple flatMap() while processing a single stream. Both map() and flatMap() both can be applied to a Stream<T> and return a Stream<R> or transformed elements, but the main difference is that map() produces one output element for each input element, whereas flatMap() produces any number (0-n) of output elements for each input element. Put simply, when using a map(), the resulting stream would have same number or elements as the input stream, where as it can be different in case of a flatMap(). The following example illustrates the differences the use of flatMap()

Monday, August 28, 2017

Java 8 Streams map()

In Java 8 Streams, map() is an intermediate function that can be used to apply a function to every element of a Stream. A few variations of map() include mapToInt(), mapToLong etc. which return streams of the corresponding types. The following examples show a few ways to use the map() function on streams.

Sunday, August 27, 2017

Java 8 Streams : map vs flatMap

In Java 8 Stream API, map and flatMap methods are provided as part of the java.util.stream.Stream Interface. Both methods are intermediate functions for streams, in that, they do not produce a final result but are used to apply transformations on the elements of a Stream. It is possible to to chain multiple map() and flatMap() while processing a single stream.

Saturday, August 26, 2017

Java 8 Streams

Java 8 Streams API provide a declarative way to process sequence of data elements. A stream can be a sequence of elements from a collection, a I/O channel or a generator function. To process a stream, a set of operations are composed into a pipeline. A stream pipeline consists of a Source (data stream), one or more intermediate operations (apply transformations on the stream) and a terminal operation that produces a result or a side-effect. The following example (from the Stream javadoc), has one source (widgets), two intermediate operations (filter and mapToInt) and a terminal operation (sum).
  int sum = widgets.stream() // Source
                    .filter(w -> w.getColor() == RED) // Intermediate operation
                    .mapToInt(w -> w.getWeight())  //Intermediate operation
                    .sum();  // Terminal operation, produces result
In this article, we will go over the basics of streams and how to create or initialize the source for a stream pipeline.

Popular Posts