Here is my code, it will work for you.

Function Reference version

public class AppLauncher {

public static void main(String a[]){
    Map<Integer,Integer> map = IntStream.range(1,10)
        .boxed().collect(Collectors.toMap(
             Function.identity(),
             AppLauncher::computeSmth
        ));
    System.out.println(map);
}
  public static Integer computeSmth(Integer i){
    return i*i;
  }
}

Lambda expression version

public class AppLauncher {

    public static void main(String a[]){
        Map<Integer,Integer> map = IntStream.range(1,10)
            .boxed().collect(Collectors.toMap(
                Function.identity(),
                i->i*i
            ));
        System.out.println(map);
    }
}
Answer from Noor Nawaz on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › IntStream.html
IntStream (Java Platform SE 8 )
March 16, 2026 - mapper - a non-interfering, stateless function to apply to each element which produces an IntStream of new values
🌐
Baeldung
baeldung.com › home › java › java collections › java map › java collectors tomap
Java Collectors toMap | Baeldung
February 26, 2026 - Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction) Let’s introduce a merge function that indicates that, in the case of a collision, we keep the existing entry: public Map<Integer, Book> listToMapWithDupKey(List<Book> books) { return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (existing, replacement) -> existing)); }
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › collecting stream of primitives into collection or array
Collecting Stream of Primitives into Collection or Array
March 14, 2022 - //Primitive Stream -> List List<Integer> listOfIntegers = IntStream.of(1,2,3,4,5) .boxed() .collect(Collectors.toList()); List<Long> listOfLongs = LongStream.of(1,2,3,4,5) .boxed() .collect(Collectors.toList()); List<Double> listOfDoubles = DoubleStream.of(1.0,2.0,3.0,4.0,5.0) .boxed() .collect(Collectors.toList()); Another way is to manually to the boxing using IntStream.mapToObj(), IntStream.mapToLong() or IntStream.mapToDouble() methods.
🌐
Tabnine
tabnine.com › home page › code › java › java.util.stream.intstream
java.util.stream.IntStream.map java code examples | Tabnine
@Override public IntStream map(IntUnaryOperator mapper) { requireNonNull(mapper); if (STRICT) { return toStream().map(mapper); } return of(mapper.applyAsInt(element)); } ... @Test public void testShuffled() { List<Integer> list = IntStream.range(0, INPUT_SIZE).collect(ArrayList::new, ArrayList::add, ArrayList::addAll); Collections.shuffle(list); test(list.stream().mapToInt(Integer::intValue), list.stream().mapToInt(Integer::intValue).mapToObj(key -> Integer.toString(key * 2)), MAX_ELEMENTS_COMPARATOR, IntStream.range(INPUT_SIZE - OUTPUT_SIZE, INPUT_SIZE).mapToObj(key -> Integer.toString(key * 2)).iterator()); test(list.stream().mapToInt(Integer::intValue), list.stream().mapToInt(Integer::intValue).mapToObj(key -> Integer.toString(key * 2)), MIN_ELEMENTS_COMPARATOR, IntStream.range(0, OUTPUT_SIZE).map(x -> OUTPUT_SIZE - 1 - x).mapToObj(key -> Integer.toString(key * 2)).iterator()); }
🌐
Baeldung
baeldung.com › home › java › java streams › java intstream conversions
Java IntStream Conversions | Baeldung
January 8, 2024 - Finally, we can use a collector to get a list of integers. For our last topic, let’s explore how we could get a String from an IntStream. In this case, we will generate just the first 3 ints (0, 1 and 2): @Test public void intStreamToString() { String first3numbers = IntStream.of(0, 1, 2) .mapToObj(String::valueOf) .collect(Collectors.joining(", ", "[", "]")); assertThat(first3numbers).isEqualTo("[0, 1, 2]"); }
🌐
CodeGym
codegym.cc › java blog › stream api › convert stream to a map in java heading
Convert Stream to a Map in Java Heading
August 31, 2023 - Let’s have a look at the toMap(Function keyMapper, Function valueMapper) method. It transforms Stream elements into a Map using the provided functions to compute keys and values. If there are duplicate keys, an exception will be thrown.
Find elsewhere
🌐
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 - import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; public class ConvertStreamToMapWithIndex { public static void main(String[] args) { Stream<String> stream = Stream.of("Java", "Python", "C++", "JavaScript"); // Convert stream to Map with index as key and element as value Map<Integer, String> map = IntStream.range(0, (int) stream.count()) // Generate indices .boxed() .collect(Collectors.toMap(i -> i, i -> stream.skip(i).findFirst().get())); System.out.println(map); // Output: {0=Java, 1=Python, 2=C++, 3=JavaScript} } } IntStream.range(0, n) generates indices for each element.
🌐
LogicBig
logicbig.com › how-to › code-snippets › jcode-java-8-streams-collectors-tomap.html
Java 8 Streams - Collectors.toMap Examples
Java 8 Streams Java Java API · The static overloaded methods, Collectors.toMap() return a Collector which produces a new instance of Map, populated with keys per provided keyMapper function and values per provided valueMap function
🌐
CodingTechRoom
codingtechroom.com › question › how-to-fix-the-intstream-collection-error-in-java-a-step-by-step-guide
How to Resolve Errors When Collecting an IntStream to a Map in Java? - CodingTechRoom
Solution: Always use .boxed() when collecting to a Map for non-primitive types. Mistake: Not handling potential key collisions in the map.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java collectors.tomap(): collecting stream items into map
Java Collectors.toMap(): Collecting Stream Items into Map
October 14, 2023 - Learn to collect Stream items into Map using Collectors.toMap() and Collectors.groupingBy() when Map keys are distinct or duplicates.
🌐
Straub
straub.as › java › history › java8 › intstreams.html
IntStreams
IntStream intStream = IntStream.of(0, 0, 1, 1, 2, 2, 3, 3, 4, 4); Set<Integer> intSet = intStream.boxed().collect( Collectors.toSet()); ... IntStream intStream = IntStream.of(0, 0, 1, 1, 2, 2, 3, 3, 4, 4); Set<Integer> intSet = intStream.mapToObj(Integer::new).collect(Collectors.toSet());
🌐
CodingTechRoom
codingtechroom.com › question › convert-intstream-to-map-java
How to Convert an IntStream to a Map in Java? - CodingTechRoom
IntStream intStream = IntStream.range(1, 10); Map<Integer, String> numberMap = intStream boxed() .collect(Collectors.toMap( Function.identity(), // Key: each integer i -> "Number: " + i // Value: formatted string )); System.out.println(numberMap); // Outputs: {1=Number: 1, 2=Number: 2, ..., 9=Number: 9}
🌐
ZetCode
zetcode.com › java › streammap
Java Stream map - map operations on Java streams
The example demonstrates how to use the mapToInt method to convert a stream of Integer objects into a stream of primitive int values. This conversion allows for efficient arithmetic operations, such as filtering even numbers and calculating their sum. The mapToInt method is particularly useful ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java 8 - Convert IntStream To List and Other - Java Code Geeks
November 27, 2021 - In java 8 API, IntStream class has boxed() method. boxed() method converts the primitive int values into a stream of integer objects. Once we get the Stream<Integer> instance then we can convert it to the List or Set or Map or any collection.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-a-stream-into-a-map-in-java
How to convert a Stream into a Map in Java - GeeksforGeeks
July 15, 2025 - Method 1: Using Collectors.toMap() Function The Collectors.toMap() method takes two parameters as the input: KeyMapper: This function is used for extracting keys of the Map from stream value.