Java IntStream map() Method

The map() 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 stream of the results. This method is useful when you need to transform the elements of a stream.

Table of Contents

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

Introduction

The map() method returns a stream consisting of the results of applying a 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.

map() Method Syntax

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

IntStream map(IntUnaryOperator mapper)

Parameters:

  • mapper: An IntUnaryOperator that represents the function to be applied to each element of the stream.

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 map()

The map() method processes each element of the stream and applies the specified function to it, resulting in a new stream containing the transformed elements. This is useful for performing operations such as mathematical transformations, object conversions, and more.

Examples

Basic Usage

To demonstrate the basic usage of map(), we will create an IntStream and use map() to square each element.

Example

import java.util.stream.IntStream;

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

        // Use map() to square each element
        IntStream squaredStream = intStream.map(n -> n * n);

        // Print the squared elements
        squaredStream.forEach(System.out::println);
    }
}

Output:

1
4
9
16
25

Using map() with Other Stream Operations

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

Example

import java.util.stream.IntStream;

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

        // Filter even numbers and map them to their squares
        IntStream processedStream = intStream.filter(n -> n % 2 == 0)
                                             .map(n -> n * n);

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

Output:

4
16
36
64

Real-World Use Case

Converting Scores to Grades

In real-world applications, the map() method can be used to convert scores to grades based on a predefined grading scale.

Example

import java.util.stream.IntStream;

public class ConvertScoresExample {
    public static void main(String[] args) {
        IntStream scores = IntStream.of(85, 92, 78, 88, 95);

        // Convert scores to grades
        IntStream grades = scores.map(score -> {
            if (score >= 90) return 4; // A
            if (score >= 80) return 3; // B
            if (score >= 70) return 2; // C
            return 1; // D
        });

        // Print the grades
        grades.forEach(grade -> System.out.println("Grade: " + grade));
    }
}

Output:

Grade: 3
Grade: 4
Grade: 2
Grade: 3
Grade: 4

Conclusion

The IntStream.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. By understanding and using this method, you can efficiently manage and process streams of integer values in your Java applications.

Comments