Death and Dying

I spend a fair amount of time considering death and dying and what it all means in the grand spectrum of this omnipotent universe I wonder if there is death or if our game just ends and we get…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Listing Immediate Subdirectories Using flatMap

Functional Programming in Java — by Venkat Subramaniam (34 / 96)

👈 Listing Select Files in a Directory | TOC | Watching a File Change 👉

We saw how to list the members of a given directory. Let’s look at the effort to explore the immediate (one level deep) subdirectories in a given directory, first using a rudimentary operation and then, more conveniently, using the flatMap method.

Let’s use the traditional for loop first to iterate over the files in a given directory. If a subdirectory contains any files, we’ll add them to our list; otherwise, we’ll add the subdirectory itself to the list. Finally, we’ll print the total number of files found. Here’s the code — for the hard way.

We fetch the list of files in the current directory and loop through each of the files. For each file, we query for its children and add them, if present, to the list of files. That works, but it comes with the usual culprits: mutability, primitive obsession, imperative, noise.… We can get rid of these using a nice little method called flatMap.

As the name indicates, this method will flatten after mapping. It maps the elements in a collection, much like the map method does. But unlike the map method, where we generally return an element from the lambda expression, we return a Stream instead. The method then flattens the multiple streams, obtained by mapping each element, into one flat stream.

We can use flatMap for various operations, but the problem on hand nicely demonstrates the usefulness of this method. Each subdirectory has a list or stream of files and we’re trying to get a combined (or flattened) list of files in all the subdirectories of the current directory.

Some directories (or files) may be empty and may not have children. In that case, we simply wrap a stream around the no-child directory or file element. In case…

Add a comment

Related posts:

Chapter 1 Your First Random Mazes

Unlock the secrets to creating random mazes! Whether you're a game developer, an algorithm connoisseur, or simply in search of a new puzzle, you're about to level up. Learn algorithms to randomly generate mazes in a variety of shapes, sizes, and dimensions. Bend them into Moebius strips, fold them into cubes, and wrap them aroun

How Losing My Hair Taught Me to Listen

My hair has always shed. I cannot remember a time when I was not shedding. I have always had thick, long blonde hair. Family and loved ones are forever picking pieces of my hair off their clothes and…

Advantages of Selenium Test Automation

The era of digital technology is moving away from generic manual testing and approaching automation testing. This is happening because of new automation tools. Selenium is one of the most popular…