Java Stream mapToInt() Method

The mapToInt() method in Java, part of the java.util.stream.Stream interface, is used to transform each element of the stream into an int-valued stream. This method is useful when you need to perform operations or calculations on int values derived from the original stream elements.

Table of Contents

  1. Introduction
  2. mapToInt() Method Syntax
  3. Understanding mapToInt()
  4. Examples
    • Basic Usage
    • Using mapToInt() with Complex Transformations
  5. Real-World Use Case
  6. Conclusion

Introduction

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

mapToInt() Method Syntax

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

IntStream mapToInt(ToIntFunction<? super T> mapper)

Parameters:

  • mapper: A function to apply to each element of the stream, which produces an int value.

Returns:

  • A new IntStream 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 mapToInt()

The mapToInt() method allows you to transform each element of the original stream into an int value. This is useful for performing numerical operations or calculations on the elements of the stream.

Examples

Basic Usage

To demonstrate the basic usage of mapToInt(), we will create a Stream of strings and use mapToInt() to transform each string into its length.

Example

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class MapToIntExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("apple", "banana", "cherry");

        // Use mapToInt() to transform each string into its length
        IntStream intStream = stream.mapToInt(String::length);

        // Print the transformed elements
        intStream.forEach(System.out::println);
    }
}

Output:

5
6
6

Using mapToInt() with Complex Transformations

This example shows how to use mapToInt() to calculate the square of each integer in a stream.

Example

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class MapToIntComplexExample {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);

        // Use mapToInt() to calculate the square of each integer
        IntStream intStream = stream.mapToInt(n -> n * n);

        // Print the transformed elements
        intStream.forEach(System.out::println);
    }
}

Output:

1
4
9
16
25

Real-World Use Case

Calculating Total Salaries

In real-world applications, the mapToInt() method can be used to calculate the total salary of a list of employees.

Example

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class MapToIntRealWorldExample {
    static class Employee {
        String name;
        int salary;

        Employee(String name, int salary) {
            this.name = name;
            this.salary = salary;
        }

        int getSalary() {
            return salary;
        }
    }

    public static void main(String[] args) {
        Stream<Employee> employees = Stream.of(
            new Employee("Alice", 50000),
            new Employee("Bob", 60000),
            new Employee("Charlie", 70000)
        );

        // Use mapToInt() to transform each employee into their salary and calculate the total
        IntStream salaries = employees.mapToInt(Employee::getSalary);
        int totalSalary = salaries.sum();

        // Print the total salary
        System.out.println("Total Salary: " + totalSalary);
    }
}

Output:

Total Salary: 180000

Conclusion

The Stream.mapToInt() method is used to transform each element of the stream into an int-valued stream. This method is particularly useful for performing numerical operations or calculations on the elements of the stream. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, performing necessary numerical transformations and calculations as needed.

Comments