Greetings, fellow adventurers of the programming realm! As we continue our expedition into the heart of functional programming and the realm of Java Streams, it’s time to unveil the mystical powers of the map operation. So, buckle up as we venture deeper into the stream landscape, exploring the captivating world of transformation.
Table of contents
Open Table of contents
Sections
Unveiling the Magic of Map
Recall our previous rendezvous with the filter operation, where we selectively plucked elements from our stream based on a specified condition. Now, let’s take a stroll down a different path – the map operation.
Map is the true magician of the stream world, armed with a versatile wand: a mapper function. This function possesses an abstract apply method, taking one input and producing one output. Unlike the filter operation, which selectively curates elements, map transforms each element into something entirely new – a feat only possible in the world of functional programming.
A Glimpse into the Enchantment
Picture this: you have a stream of integers, and you yearn to conjure the multiplication table of a chosen number. Behold, the map operation at your service:
Stream<Integer> numbers = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> multiplicationTable = numbers.map(n -> n * 25)
.collect(Collectors.toList());
multiplicationTable.forEach(System.out::println);
Here’s the spell we’re casting:
1 - We create a stream of integers. 2 - The map operation takes center stage. Armed with a mapper function n -> n * 25, it multiplies each integer by 25, conjuring a transformed stream of elements. 3 - With our transformed stream in hand, we collect the magic – well, data – into a list using the collect method with Collectors.toList(). Our incantation culminates with a terminal operation: forEach. This operation prints each transformed integer to the console.
Mapping the Familiar
Let’s revisit our trusty old friend – the book example. Recall how we filtered out horror books with ratings greater than three. Now, let’s spice things up by mapping the stream to reveal the names of these captivating tales:
List<Book> books = /* Your list of Book objects */;
List<String> bookNames = books.stream()
.filter(book -> book.getGenre().equalsIgnoreCase("Horror"))
.filter(book -> book.getRating() > 3)
.map(Book::getName)
.collect(Collectors.toList());
bookNames.forEach(System.out::println);
With the map operation, we transform each book object into its name, culminating in a stream of book names. Our functional journey not only filters but also metamorphoses the very essence of our data.
Navigating the Stream Wisely
While mapping holds a potent allure, tread carefully in the world of functional programming. Remember, the essence of functional programming lies in immutability and avoiding state changes. Thus, when utilizing the map operation, opt for transformation rather than mutation.
The magic of the map operation lies in its power to transform, sculpt, and reshape data without altering its core essence. As you navigate the stream realm, let the enchanting capabilities of map fuel your creativity and elevate your code to new heights.
Our expedition continues, fellow explorers, as we unravel more captivating stream operations that guide us toward a landscape of elegance, efficiency, and functional beauty.
Until our paths cross again, may your code remain fluid, your transformations profound, and your spirit adventurous.
You can find the repo for this section of the course Here