Java8 mapToObj method
Hi Friends,
I am gonna to explain about mapToObj in Java8, its very basic topic.
In Java 8, mapToObj
is a method available in the primitive streams (IntStream
, LongStream
, DoubleStream
) to map each element of the stream to an object. This is particularly useful when you need to transform primitive values into their corresponding object forms or other complex types.
Usage
Here’s a detailed look at mapToObj
for IntStream
:
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
Stream<String> stringStream = intStream.mapToObj(Integer::toString);
In this example:
IntStream.of(1, 2, 3, 4, 5)
creates a stream of integers.mapToObj(Integer::toString)
maps each integer to its string representation.
General Syntax
The general syntax of mapToObj
is:
mapToObj(IntFunction<R> mapper)
Where IntFunction<R>
is a functional interface that takes an int
as input and produces an object of type R
.
Example
Here’s an example to demonstrate mapToObj
with a more practical scenario:
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class MapToObjExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
Stream<String> stringStream = IntStream.of(numbers)
.mapToObj(n -> "Number " + n);
stringStream.forEach(System.out::println);
}
}
In this example:
IntStream.of(numbers)
creates a stream of integers from the arraynumbers
.mapToObj(n -> "Number " + n)
maps each integer to a string prefixed with "Number ".forEach(System.out::println)
prints each string.
Explanation with Filtering Example
Let’s revisit the code you mentioned and explain it further in the context of filtering numbers that start with “1”:
import java.util.Arrays;
public class StartsWithOne {
public static void main(String[] args) {
int[] arr = {10, 12, 23, 34, 45, 51, 13, 16};
Arrays.stream(arr)
.mapToObj(s -> s + "")
.filter(s -> s.startsWith("1"))
.forEach(System.out::println);
}
}
Detailed steps:
- Convert Array to Stream:
Arrays.stream(arr)
creates a stream from the arrayarr
. Ifarr
is anint[]
, it produces anIntStream
. - Mapping to Strings:
.mapToObj(s -> s + "")
converts each integer in the stream to its string representation. - Filtering:
.filter(s -> s.startsWith("1"))
filters out the strings that start with "1". - Printing:
.forEach(System.out::println)
prints each element of the filtered stream.
This process ensures that only the numbers starting with “1” (when converted to strings) are printed.
Example : Filtering and Printing Even Numbers as Strings
import java.util.stream.IntStream;
public class FilterEvenNumbers {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6};
IntStream.of(numbers)
.filter(n -> n % 2 == 0)
.mapToObj(Integer::toString)
.forEach(System.out::println);
}
}
Output:
2
4
6
Example : Converting Integers to Their Square Strings
This example converts each integer in the stream to a string representing its square.
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class SquareStrings {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
Stream<String> squareStrings = IntStream.of(numbers)
.mapToObj(n -> "Square of " + n + " is " + (n * n));
squareStrings.forEach(System.out::println);
}
}
Thanks,
Happy Learning.