Open In App

IntStream map(IntUnaryOperator mapper) in Java

Last Updated : 06 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

IntStream map(IntUnaryOperator mapper) returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.

Syntax :

IntStream map(IntUnaryOperator mapper)

Parameters :

  1. IntStream : A sequence of primitive int-valued elements.
  2. IntUnaryOperator : An operation on a single int-valued operand that produces an int-valued result.
  3. mapper : A stateless function which is applied to each element and the function returns the new stream.

Return Value : IntStream map(IntUnaryOperator mapper) returns a stream by applying the given function.

Example 1 : Using IntStream map() to get the negative of square of elements of IntStream.




// Java code for IntStream map
// (IntUnaryOperator mapper) to get a
// stream by applying the given function.
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream1 = IntStream.of(4, 5, 6, 7);
  
        // Using IntStream map()
        IntStream stream2 = stream1.map(num -> (-num * num));
  
        // Displaying the resulting IntStream
        stream2.forEach(System.out::println);
    }
}


Output:

-16
-25
-36
-49

Example 2 :Using IntStream map() to get the half of elements of IntStream in given range.




// Java code for IntStream map
// (IntUnaryOperator mapper) to get a
// stream by applying the given function.
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream in range [2, 7)
        IntStream stream1 = IntStream.range(2, 7);
  
        // Using IntStream map()
        IntStream stream2 = stream1.map(num -> (num / 2));
  
        // Displaying the resulting IntStream
        stream2.forEach(System.out::println);
    }
}


Output:

1
1
2
2
3


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads