Java LongStream map() Method

The map() method in Java, part of the java.util.stream.LongStream interface, is used to apply a given function to each element of the stream, producing a new stream of the results. This method is useful for transforming the elements of a stream into a new form.

Table of Contents

  1. Introduction
  2. map() Method Syntax
  3. Understanding map()
  4. Examples
    • Basic Usage
    • Using map() with a Custom Function
  5. Real-World Use Case
  6. Conclusion

Introduction

The map() method transforms each element of the stream by applying a given function, resulting in a new stream of transformed elements. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.

map() Method Syntax

The syntax for the map() method is as follows:

LongStream map(LongUnaryOperator mapper)

Parameters:

  • mapper: A LongUnaryOperator that represents the function to be applied to each element of the stream.

Returns:

  • A new LongStream consisting of the results of applying the given function to the elements of the original stream.

Throws:

  • This method does not throw any exceptions.

Understanding map()

The map() method allows you to transform each element of a LongStream by applying a specified function to it. The resulting stream contains the transformed elements, each of which is the result of applying the function to the corresponding element in the original stream.

Examples

Basic Usage

To demonstrate the basic usage of map(), we will create a LongStream and use map() to double each element.

Example

import java.util.stream.LongStream;

public class MapExample {
    public static void main(String[] args) {
        LongStream stream = LongStream.of(1L, 2L, 3L, 4L, 5L);

        // Use map() to double each element
        LongStream doubledStream = stream.map(n -> n * 2);

        // Print the elements of the doubled stream
        doubledStream.forEach(System.out::println);
    }
}

Output:

2
4
6
8
10

Using map() with a Custom Function

This example shows how to use map() with a custom function to transform each element of the stream.

Example

import java.util.stream.LongStream;

public class MapCustomFunctionExample {
    public static void main(String[] args) {
        LongStream stream = LongStream.of(10L, 20L, 30L, 40L, 50L);

        // Use map() with a custom function to add 100 to each element
        LongStream transformedStream = stream.map(n -> n + 100);

        // Print the elements of the transformed stream
        transformedStream.forEach(System.out::println);
    }
}

Output:

110
120
130
140
150

Real-World Use Case

Transforming Transaction Amounts

In real-world applications, the map() method can be used to apply transformations to transaction amounts, such as applying a discount or tax rate.

Example

import java.util.stream.LongStream;

public class MapTransactionsExample {
    public static void main(String[] args) {
        LongStream transactionAmounts = LongStream.of(1000L, 2000L, 1500L, 3000L, 2500L);

        double taxRate = 0.1;

        // Use map() to apply a tax rate to each transaction amount
        LongStream taxedAmounts = transactionAmounts.map(amount -> (long)(amount * (1 + taxRate)));

        // Print the taxed transaction amounts
        taxedAmounts.forEach(System.out::println);
    }
}

Output:

1100
2200
1650
3300
2750

Conclusion

The LongStream.map() method is used to apply a given function to each element of the stream, producing a new stream of the results. This method is particularly useful for transforming the elements of a stream into a new form. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that each element is transformed as needed.

Comments