Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Stream.mapToInt() method in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The mapToInt() method of the Stream interface is used to return an IntStream that contains the results of applying the provided implementation of the ToIntFunction() function on the stream’s items.

Syntax


IntStream mapToInt(ToIntFunction<? super T> mapper)

Parameters

  • ToIntFunction<? super T> mapper: This is the mapper function that needs to be applied.

Return value

This method returns a new stream.

Code

import java.util.Arrays;
import java.util.List;
import java.util.function.ToIntFunction;
public class Main{
public static void main(String[] args) {
// define the list of integers
List<Integer> integerList = Arrays.asList(10, 24, 54, 8,232);
// Define the implementation of ToIntFunction interface
ToIntFunction<Integer> multiplyByTwo = e -> e * 2;
// apply the multiplyByTwo using mapToInt function and print each value
integerList.stream().mapToInt(multiplyByTwo).forEach(System.out::println);
}
}
Example of the "stream.mapToInt()" method

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring