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
🌐
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 - Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
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
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
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
🌐
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 - // Convert String or String array to HashMap In Java // Phase 1: Input- String converted to ArrayofStrings // Importing java generic libraries import java.util.*; class GFG { // Main driver method public static void main(String[] args) { // String object that contains the student name and // roll number separated by a comma String student = "Aashish:1, Bina:2, Chintu:3"; // New HashMap obj Map<String, String> hashMap = new HashMap<String, String>(); // split the String by a comma String parts[] = student.split(","); // iterate the parts and add them to a HashMap for (String part : parts) { // split the student data by colon to get the // name and roll number String stuData[] = part.split(":"); String stuRollNo = stuData[0].trim(); String stuName = stuData[1].trim(); // Add to map hashMap.put(stuRollNo, stuName); } // Print hashMap System.out.println("String to HashMap: " + hashMap); } }
🌐
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?

Find elsewhere
🌐
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 - This process is particularly useful when you have serialized data of key-value pairs in a string format, and you need to transform it into a more structured data structure like a Map. If we have a String containing key-value pairs separated by a delimiter such as a comma or semicolon, We can use Java Streams to convert it into a Map.
🌐
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
This is the best way to convert a List to Map in pre-JDK 8 worlds. It's clear, concise, and self-explanatory, but iterative. 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 example, the stream() method returns a stream of Book object from the list, and then I have used collect() method of Stream class to collect all elements.
🌐
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 java.util.Arrays; import java.util.Map; class GFG { // Function to convert the string // to the map public static Map toMap(String input) { Map<String, Integer> lengthMap = Arrays.stream(input.split(" ")) .collect(Collectors.toMap( value -> value, value -> value.length())); return lengthMap; } public static void main(String[] args) { String input = "Geeks for Geek"; System.out.println(toMap(input)); } }
🌐
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.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Split String To Map In Java - Java Code Geeks
April 3, 2024 - One way to achieve this is by using the StringTokenizer class. Suppose we have a string containing key-value pairs separated by a custom delimiter :. We will use StringTokenizer to split the string and create a map of key-value pairs.
🌐
Mkyong
mkyong.com › home › java8 › java 8 – convert list to map
Java 8 – Convert List to Map | mkyong.com
May 26, 2017 - Few Java 8 examples to show you how to convert a List of objects into a Map, and how to handle the duplicated keys. Hosting.java · package com.mkyong.java8 public class Hosting { private int Id; private String name; private long websites; public Hosting(int id, String name, long websites) { Id = id; this.name = name; this.websites = websites; } //getters, setters and toString() } Create a list of the Hosting objects, and uses Collectors.toMap to convert it into a Map.
🌐
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:
🌐
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Map.html
Map (Java Platform SE 8 )
July 21, 2026 - Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). For example, to either create or append a String msg to a value mapping: