A Stream API Example In Java 8

Streams

To start with, we will go over a simple yet interesting problem in Java: finding the second smallest value in an array. There are a few different approaches we could take. The most obvious would be sorting the array, skipping the first element (and any duplicates). However, a more efficient approach could be taken as follows:

First, instantiate a simple int array and find the minimum as you usually would.

Then, simply filter out that first minimum value from the array and find the minimum of that subarray respectively.

This approach saves on time complexity and feels more readable in my personal opinion. I thought it would be interesting to share this cool snippet with everyone!

Coming Soon in Part 2: Information about CompletableFuture in Java 8!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Auto