Arrays.stream with an int[] will give you an IntStream so map will expect a IntUnaryOperator (a function int -> int).

The function you provide is of the type int -> T where T is an object (it would work if T was an Integer due to unboxing, but not for an unbounded generic type parameter, assuming it's a generic type).

What you are looking for is to use mapToObj instead, which expects an IntFunction (a function int -> T) and gives you back a Stream<T>:

//you might want to use the overloaded toArray() method also.
Arrays.stream(permutation).mapToObj(i -> source[i]).toArray();
Answer from Alexis C. on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › Stream.html
Stream (Java Platform SE 8 )
2 weeks ago - Returns an DoubleStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have placed been into this stream.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java stream map()
Java Stream map() with Examples - HowToDoInJava
August 26, 2023 - Learn to use Java Stream map() method which produces one output value of a different type ‘X’ for each input value of type ‘Y’.
🌐
Medium
medium.com › @AlexanderObregon › javas-stream-map-method-explained-df0d0d461d39
Java’s Stream.map() Method Explained | Medium
August 27, 2024 - The map() method is a key part of Java's Stream API. It allows you to apply a given function to each element of the stream, producing a new stream with the transformed elements. This method is particularly useful in scenarios where you need ...
🌐
Javabrahman
javabrahman.com › java-8 › java-8-mapping-with-streams-map-flatmap-methods-tutorial-with-examples
Java 8 Mapping with Streams | map and flatMap methods ...
map() method is used to convert the Stream of Employees to a Stream of Strings with just the employee names. Conversion from Employee type to String type is achieved using a Function
🌐
Java Development Journal
javadevjournal.com › home › java streams map() examples
Java Streams map() Examples | Java Development Journal
May 19, 2020 - Stream map() is an intermediate ... each value of the stream and returns one fresh stream. In this tutorial, we will learn how to use the Java Streams map function with different examples....
🌐
codippa
codippa.com › home › java 8 stream map() method
Java 8 stream map() to convert one object to another
January 11, 2025 - map() method in java 8 stream is used to convert an object of one type to an object of another type or to transform elements of a collection.
🌐
ZetCode
zetcode.com › java › streammap
Java Stream map - map operations on Java streams
Converting one data type to another (e.g., String to Integer). ... Immutable Processing: Original data remains unchanged. Functional Approach: Reduces boilerplate code for transformations. Performance Optimization: Works efficiently on large datasets. Seamless Integration: Can be combined with other stream operations like filter and reduce. The map method is a powerful tool for applying transformations to data within Java ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java collections › java map › working with maps using streams
Working With Maps Using Streams | Baeldung
November 10, 2025 - Learn different techniques for merging maps in Java 8 ... Learn how to use the toMap() method of the Collectors class. ... The article is an example-heavy introduction of the possibilities and operations offered by the Java 8 Stream API.
🌐
LabEx
labex.io › tutorials › java-how-to-use-java-streams-for-type-conversion-464452
How to use Java streams for type conversion | LabEx
For hands-on practice with Java Streams, LabEx offers comprehensive coding environments to help developers master stream operations effectively. Type conversion is a crucial aspect of stream processing, allowing developers to transform data between different types efficiently and elegantly. graph TD A[Type Conversion Methods] --> B[map()] A --> C[mapToInt()] A --> D[mapToDouble()] A --> E[mapToLong()] A --> F[collect()]
🌐
Stack Abuse
stackabuse.com › java-8-stream-map-examples
Java 8 - Stream.map() Examples
February 23, 2023 - In this tutorial, we'll go over the map() operation and how we can use it with Streams to convert/map objects of various types.
🌐
Mkyong
mkyong.com › home › java8 › java 8 streams map() examples
Java 8 Streams map() examples - Mkyong.com
April 3, 2017 - package com.mkyong.java8; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class TestJava8 { public static void main(String[] args) { List<String> alpha = Arrays.asList("a", "b", "c", "d"); //Before Java8 List<String> alphaUpper = new ArrayList<>(); for (String s : alpha) { alphaUpper.add(s.toUpperCase()); } System.out.println(alpha); //[a, b, c, d] System.out.println(alphaUpper); //[A, B, C, D] // Java 8 List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList()); System.out.println(collect); //[A, B, C, D] // Extra, streams apply to any data type.
🌐
Scaler
scaler.com › home › topics › java stream map()
Java Stream map() - Scaler Topics
June 22, 2024 - The syntax of the Java Stream map() method is as follows - ... R - It denotes the element type of the new Stream. ? super T - It represents that the original Stream should have the values that are a superclass of type T. ? extends R - It allows the method taken by map to be declared as returning a subtype of R mapper - It is the stateless and non-interfering method that gets applied to each element of the Stream and produces a new Stream of values.
🌐
Level Up Lunch
leveluplunch.com › java › tutorials › 016-transform-object-class-into-another-type-java8
Transform object into another type with Java 8 | Level Up Lunch
November 24, 2014 - @Test public void convertCollection() { GoogleGeoLocation googleLocation1 = new GoogleGeoLocation(310, 410, "gsm", "Vodafone"); GoogleGeoLocation googleLocation2 = new GoogleGeoLocation(222, 777, "gprs", "Verizon"); GoogleGeoLocation googleLocation3 = new GoogleGeoLocation(111, 234, "gsm", "Sprint"); GoogleGeoLocation googleLocation4 = new GoogleGeoLocation(345, 567, "gprs", "Us Cell"); List<GoogleGeoLocation> gLocations = new ArrayList<GoogleGeoLocation>(); gLocations.add(googleLocation1); gLocations.add(googleLocation2); gLocations.add(googleLocation3); gLocations.add(googleLocation4); List<MyLocation> myLocations = gLocations.stream() .map(externalToMyLocation) .collect(Collectors.<MyLocation> toList()); System.out.println(myLocations); }
🌐
Stackify
stackify.com › streams-guide-java-8
A Guide to Java Streams: In-Depth Tutorial With Examples
September 4, 2024 - map() produces a new stream after applying a function to each element of the original stream. The new stream could be of a different type.
🌐
JRebel
jrebel.com › blog › java-streams-in-java-8
Using Java Streams in Java 8 and Beyond | JRebel by Perforce
Map: Transforms the stream elements into something else, it accepts a function to apply to each and every element of the stream and returns a stream of the values the parameter function produced.
Top answer
1 of 10
520
Map<String, String> x;
Map<String, Integer> y =
    x.entrySet().stream()
        .collect(Collectors.toMap(
            e -> e.getKey(),
            e -> Integer.parseInt(e.getValue())
        ));

It's not quite as nice as the list code. You can't construct new Map.Entrys in a map() call so the work is mixed into the collect() call.

2 of 10
38

Here are some variations on Sotirios Delimanolis' answer, which was pretty good to begin with (+1). Consider the following:

static <X, Y, Z> Map<X, Z> transform(Map<? extends X, ? extends Y> input,
                                     Function<Y, Z> function) {
    return input.keySet().stream()
        .collect(Collectors.toMap(Function.identity(),
                                  key -> function.apply(input.get(key))));
}

A couple points here. First is the use of wildcards in the generics; this makes the function somewhat more flexible. A wildcard would be necessary if, for example, you wanted the output map to have a key that's a superclass of the input map's key:

Map<String, String> input = new HashMap<String, String>();
input.put("string1", "42");
input.put("string2", "41");
Map<CharSequence, Integer> output = transform(input, Integer::parseInt);

(There is also an example for the map's values, but it's really contrived, and I admit that having the bounded wildcard for Y only helps in edge cases.)

A second point is that instead of running the stream over the input map's entrySet, I ran it over the keySet. This makes the code a little cleaner, I think, at the cost of having to fetch values out of the map instead of from the map entry. Incidentally, I initially had key -> key as the first argument to toMap() and this failed with a type inference error for some reason. Changing it to (X key) -> key worked, as did Function.identity().

Still another variation is as follows:

static <X, Y, Z> Map<X, Z> transform1(Map<? extends X, ? extends Y> input,
                                      Function<Y, Z> function) {
    Map<X, Z> result = new HashMap<>();
    input.forEach((k, v) -> result.put(k, function.apply(v)));
    return result;
}

This uses Map.forEach() instead of streams. This is even simpler, I think, because it dispenses with the collectors, which are somewhat clumsy to use with maps. The reason is that Map.forEach() gives the key and value as separate parameters, whereas the stream has only one value -- and you have to choose whether to use the key or the map entry as that value. On the minus side, this lacks the rich, streamy goodness of the other approaches. :-)

🌐
Medium
neesri.medium.com › how-to-convert-a-stream-to-list-set-map-and-different-types-of-collections-1902c849e34a
How to Convert a Stream to List, Set, Map and different types of collections. | by A cup of JAVA coffee with NeeSri | Medium
July 25, 2025 - In Java, Streams are powerful tools that allow you to process collections in a functional way. You can convert a Stream into different types of collections like List, Set, or Map using the Collectors class.