Javatpoint Logo
Javatpoint Logo

mapToInt() in Java

In Java, the Stream API is responsible for storing the mapToInt() method which was introduced in the Java 8 version. The main purpose of mapToInt() method is used for the transformation of the elements from a stream into an IntStream.

Let's understand about mapToInt() method in detail in the following paragraphs.

Java mapToInt() of Stream API

In Java, the mapToInt() method is majorly utilized when the user is dealing with values that are primitive int rather than regular objects. In other words, the mapToInt() method is primarily used for applying a mapping function on every individual element present in the stream, and converts them into an int value.

The mapToInt() method consists of two method signatures that will be discussed later in the paragraphs. The user is responsible for specifying the mapping function and this must be of the ToIntFunction< super T > type. In the previously mentioned syntax, T is used for representing the type of elements which are available in the original stream. The result generated by the mapToInt() method will be an IntStream which stores an int values.

One of the major advantages of using the mapToInt() method in Java is that it provides potential related to performance improvement. Users can lead to highly efficient memory usage while working with primitive data types. Also, it provides higher performance when compared to dealing with objects of streams. When the user deals with large datasets or performance-based applicatoins, then the usage of mapToInt() method could be particularly beneficial.

For instance, during operations related to mathematics or statics the usage of mapToInt() could be proved highly efficient and useful. Additionally, various types of methods are provided by Java for the resulting IntStream which can be utilized for tailoring and operating with int values. Few methods or operations provided are sum, max, min, average, etc.

Let's understand about the mapToInt() method in detail with the help of a Java example program.

Filename: MapToIntExample.java

Output:

Original List of Students:
CollegeStudent{student_name='Eshwar', student_age=20, student_subjects=[Math, Physics]}
CollegeStudent{student_name='Sreeram', student_age=22, student_subjects=[Computer Science, Java]}
CollegeStudent{student_name='Arti', student_age=21, student_subjects=[English, History]}
CollegeStudent{student_name='Meghana', student_age=23, student_subjects=[Chemistry, Biology]}

Ages of Students using mapToInt:
20 22 21 23

Example 2:

Filename: MapToIntExample1.java

Output:

Original List of Employees:
OfficeEmployee{EmployeeName=Eshwar, EmployeeSalary=50000}
OfficeEmployee{EmployeeName='Sreeram', EmployeeSalary=60000}
OfficeEmployee{EmployeeName='Arti', EmployeeSalary=75000}
OfficeEmployee{EmployeeName='Meghana', EmployeeSalary=90000}

Bonus Amounts using mapToInt:
5000 6000 7500 9000

Salary Ranges using mapToInt:
1 2 2 3






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA