You were almost correct with groupingBy, but the second parameter of that is a Collector, not a Function. Thus I used toMap.

 Map<String, Integer> map = Stream.of("bla", "blabla", "blablabla").distinct()
            .collect(Collectors.toMap(Function.identity(), String::length));
Answer from Eugene on Stack Overflow
Discussions

Converting string to Map<Integer,String> in java 8 - Stack Overflow
Can someone please guide me on how to achieve the below using Java 8. I don't know how to get that counter as the key String str = "abcd"; Map map = new HashMap (); ... More on stackoverflow.com
🌐 stackoverflow.com
February 20, 2019
Convert String array to Map using Java 8 Lambda expressions - Stack Overflow
Is there a better functional way of converting an array of Strings in the form of "key:value" to a Map using the Java 8 lambda syntax? More on stackoverflow.com
🌐 stackoverflow.com
java - List of String to Map Java8 - Stack Overflow
I have a list of String in my application. Every String is actually an output of 2 different strings concatenated by ".". e.g. Stack.Overflow Now, I am trying to convert this list to a Map with key More on stackoverflow.com
🌐 stackoverflow.com
Java 8 streams - String to HashMap counter of characters
CharSequence.chars() gives you an IntStream whose collect(...) method isn't compatible with Collectors.groupingBy(...)). However you should be able to .mapToObj(c -> c) before the collect to get a Stream that can be collected that way (at the expense of what could be a lot of boxing). More on reddit.com
🌐 r/learnjava
1
1
August 22, 2018
🌐
Baeldung
baeldung.com › home › java › java string › converting string or string array to map in java
Converting String or String Array to Map in Java | Baeldung
January 8, 2024 - In this article, we have explored different techniques for converting Strings and String arrays into Maps in Java. We covered basic conversions, handling duplicate keys, and provided JUnit test examples to ensure the correctness of the implementations.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Converting String or String Array to Map in Java - Java Code Geeks
October 10, 2023 - We can also use the String.split() method to convert a string to a map in Java. This involves splitting the input string into tokens based on a delimiter, and then parsing these tokens into key-value pairs. Here is an example:
🌐
KapreSoft
kapresoft.com › java › 2023 › 05 › 03 › java-string-to-map.html
Java • Converting Strings To Map | KapreSoft
May 3, 2023 - In conclusion, working with key-value pairs that are delimited by a specific character can be a common task in Java programming. By using the Map data structure and the Stream API, you can easily and efficiently convert a string of key-value pairs into a Map.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-string-or-string-array-to-hashmap-in-java
Convert String or String Array to HashMap In Java - GeeksforGeeks
July 23, 2025 - // Importing java generic libraries import java.util.*; class GFG { // Main driver method public static void main(String[] args) { // String array that contains the student name String stuName[] = { "Aashish", "Bina", "Chintu" }; // Integer ...
🌐
Reddit
reddit.com › r/learnjava › java 8 streams - string to hashmap counter of characters
r/learnjava on Reddit: Java 8 streams - String to HashMap counter of characters
August 22, 2018 -

Given a String, I want to create a hashmap that maps a Character to the number of times it shows up in the string.

I saw in Stack Overflow the following line:

Map<String, Long> map =
    Arrays.stream(s1.split("")).
            collect(Collectors.groupingBy(c -> c, Collectors.counting()));

And it works great, but it maps a String to the counter.

I tried writing it for Characters, and as far as I understood, there isn't a char stream, and I'll have to use IntStream. So I converted it to an IntStream, and used the same Collectors function.

Map<Integer, Long> map =
    s1.chars().
            collect(Collectors.groupingBy(c -> c, Collectors.counting()));

The above doesn't work. The error is:

Error:(14, 27) java: method collect in interface java.util.stream.IntStream cannot be applied to given types;

required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>

found: java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.Map<java.lang.Object,java.lang.Long>>

reason: cannot infer type-variable(s) R

(actual and formal argument lists differ in length)

What is the difference between the two?

🌐
javaspring
javaspring.net › blog › convert-string-array-to-map-using-java-8-lambda-expressions
How to Convert String Array to Map Using Java 8 Lambda Expressions: A Functional Approach — javaspring.net
Before diving into conversions, let’s recap two core Java 8 features we’ll use: A stream is a sequence of elements that supports functional-style operations like filtering, mapping, and collecting. For a string array, we convert it to a stream using Arrays.stream(array), which allows us to process elements sequentially.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 04 › 10-examples-of-converting-list-to-map.html
10 Examples of Converting a List to Map in Java 8
Now, let's see how we can do the same in Java 8 by using lambda expression and Stream API, here is my first attempt: Map<String, Book> result = books.stream() .collect(Collectors.toMap(book -> book.getISBN, book -> book)); In the above code ...
🌐
Medium
neesri.medium.com › what-is-map-in-java-8-f9f334367f58
What is map in java 8. In Java 8, the map method is part of… | by A cup of JAVA coffee with NeeSri | Medium
January 15, 2024 - .map(String::toUpperCase) applies the toUpperCase method to each element of the stream, transforming each string to its uppercase version.
🌐
YouTube
youtube.com › watch
Convert List into map using java 8 Streams | key should be String value should be length of String - YouTube
📌Java 8 Streams: Convert List into map using java 8 Streams 📌Please do subscribe my channel: https://www.youtube.com/c/javashastra💛 Join my telegram chann...
Published: June 6, 2023
🌐
Java Guides
javaguides.net › 2023 › 10 › convert-json-string-to-map-in-java.html
Convert JSON string to Map in Java
October 26, 2023 - Note: The generic Map.class was used, implying the keys are Strings, but values are treated as Objects. This allows for flexibility in handling different data types within the JSON string. Below is the code to convert a complex JSON string to a Java Map:
🌐
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 - Example 1: Here, we will convert a string into a Map with the keys as the words of the string and the value as the length of each word. ... // Program to convert // the Stream to Map import java.io.*; import java.util.stream.*; import ...
🌐
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.math.BigDecimal; 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<Staff> staff = Arrays.asList( new Staff("mkyong", 30, new BigDecimal(10000)), new Staff("jack", 27, new BigDecimal(20000)), new Staff("lawrence", 33, new BigDecimal(30000)) ); //Before Java 8 List<String> result = new ArrayList<>(); for (Staff x : staff) { result.add(x.getName()); } System.out.println(result); //[mkyong, jack, lawrence] //Java 8 List<String> collect = staff.stream().map(x -> x.getName()).collect(Collectors.toList()); System.out.println(collect); //[mkyong, jack, lawrence] } } 3.1 This example shows you how to convert a list of staff objects into a list of StaffPublic objects.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Split String To Map In Java - Java Code Geeks
April 3, 2024 - In this example, the input string name:John age:30 city:New York is split into key-value pairs using the : delimiter, and then stored in a HashMap. The output displays the resulting key-value map.
🌐
Techie Delight
techiedelight.com › home › java › convert a stream to a map in java
Convert a Stream to a Map in Java | Techie Delight
July 7, 2026 - We can use the Java 8 Stream to construct maps by obtaining stream from static factory methods like Stream.of() or Arrays.stream() and accumulating the stream elements into a new map using collectors.