Java IntStream mapToLong() Method

The mapToLong() method in Java, part of the java.util.stream.IntStream interface, is used to apply a given function to each element of the stream, producing a new LongStream of the results. This method is useful when you need to transform the elements of an IntStream into long values.

Table of Contents

  1. Introduction
  2. mapToLong() Method Syntax
  3. Understanding mapToLong()
  4. Examples
    • Basic Usage
    • Using mapToLong() with Other Stream Operations
  5. Real-World Use Case
  6. Conclusion

Introduction

The mapToLong() method returns a LongStream consisting of the results of applying a given function to the elements of the original IntStream. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.

mapToLong() Method Syntax

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

LongStream mapToLong(IntToLongFunction mapper)

Parameters:

  • mapper: An IntToLongFunction 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 mapToLong()

The mapToLong() method processes each element of the stream and applies the specified function to it, resulting in a new stream containing long values. This is useful for performing operations that require conversions or transformations to long values.

Examples

Basic Usage

To demonstrate the basic usage of mapToLong(), we will create an IntStream and use mapToLong() to convert each integer to its corresponding long value.

Example

import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class MapToLongExample {
    public static void main(String[] args) {
        IntStream intStream = IntStream.of(1, 2, 3, 4, 5);

        // Use mapToLong() to convert each integer to a long value
        LongStream longStream = intStream.mapToLong(n -> (long) n);

        // Print the long values
        longStream.forEach(System.out::println);
    }
}

Output:

1
2
3
4
5

Using mapToLong() with Other Stream Operations

This example shows how to use mapToLong() in combination with other stream operations, such as filtering.

Example

import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class MapToLongWithOtherOperationsExample {
    public static void main(String[] args) {
        IntStream intStream = IntStream.range(1, 10);

        // Filter even numbers and map them to their long values
        LongStream processedStream = intStream.filter(n -> n % 2 == 0)
                                              .mapToLong(n -> (long) n);

        // Print the processed elements
        processedStream.forEach(System.out::println);
    }
}

Output:

2
4
6
8

Real-World Use Case

Converting Prices to Long Values

In real-world applications, the mapToLong() method can be used to convert prices (integer values) to long values for calculations that require the long data type.

Example

import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class ConvertPricesExample {
    public static void main(String[] args) {
        IntStream prices = IntStream.of(100, 200, 300, 400, 500);

        // Convert prices to long values
        LongStream longPrices = prices.mapToLong(price -> (long) price);

        // Print the long prices
        longPrices.forEach(longPrice -> System.out.println("Long price: " + longPrice));
    }
}

Output:

Long price: 100
Long price: 200
Long price: 300
Long price: 400
Long price: 500

Conclusion

The IntStream.mapToLong() method is used to apply a given function to each element of the stream, producing a new LongStream of the results. This method is particularly useful for transforming the elements of an IntStream into long values. By understanding and using this method, you can efficiently manage and process streams of integer values in your Java applications, performing necessary transformations to long values when needed.

Comments