The mapToInt() method in Java is a part of the java.util.stream.Stream interface and is used to transform each element of the stream into an int-valued stream. In this guide, we will learn how to use mapToInt() method in Java with practical examples and real-world use cases to better understand its usage.
Table of Contents
- Introduction
mapToInt()Method Syntax- Examples
- Basic Usage
- Using
mapToInt()with Complex Transformations
- Real-World Use Case
- Conclusion
Introduction
The Stream.mapToInt() method in Java converts each element of a stream into an int value, returning an IntStream. It is designed for handling primitive int values more efficiently.
mapToInt() is commonly used when transforming a stream of objects into integers, making it ideal for tasks like counting, calculations, or statistical operations.
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 anintvalue.
Returns:
- A new
IntStreamconsisting of the results of applying the given function to the elements of the original stream.
Throws:
- This method does not throw any exceptions.
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.