Unless Splitter is doing any magic, the getTokenizeString method is obsolete here. You can perform the entire processing as a single operation:

Map<String,String> map = Pattern.compile("\\s*-\\s*")
    .splitAsStream(responseString.trim())
    .map(s -> s.split("~", 2))
    .collect(Collectors.toMap(a -> a[0], a -> a.length>1? a[1]: ""));

By using the regular expression \s*-\s* as separator, you are considering white-space as part of the separator, hence implicitly trimming the entries. There’s only one initial trim operation before processing the entries, to ensure that there is no white-space before the first or after the last entry.

Then, simply split the entries in a map step before collecting into a Map.

Answer from Holger on Stack Overflow
🌐
Coderanch
coderanch.com › t › 714600 › java › Splitting-string-Hashmap-Java-Streams
Splitting a string and then to Hashmap using Java 8 Streams (Features new in Java 8 forum at Coderanch)
August 16, 2019 - Line 14 changes the Steam to a Stream<String[]> by correct use of the split() method. If you look here (Java™ Tutorials), you will find that “:” isn't a metacharacter. You were right to start line 16 with .collect(), but that needs an argument. It is usual to pass a Collector reference; ...
🌐
Medium
parathan.medium.com › split-map-a-string-in-java-bdb006ef45f9
Split & Map a String in Java
September 27, 2018 - import java.util.*; public class App { public static void main (String args[]) { Map<String, String> map = new HashMap<String, String>(); String gotData = "key1=value1;key2=value2;key3=value3";if(gotData == ""){ map = null; }else { for(String keyValue : gotData.split(" *; *")) { String[] key = keyValue.split(" *= *"); map.put(key[0], key[1]); } } Map<String,String> finalOutput= map; System.out.println(finalOutput); } }
🌐
Baeldung
baeldung.com › home › java › java string › split java string into key-value pairs
Split Java String Into Key-Value Pairs | Baeldung
January 8, 2024 - We can use Java Sreams to break text into key-value pairs cleanly if we use Java 8 or later. ... @Test public void givenStringData_whenUsingJavaMap_thenSplitAndValidate() { String data = "name=John age=30 city=NewYork"; Map<String, String> keyValueMap = Arrays.stream(data.split(" ")) .map(kv -> kv.split("=")) .filter(kvArray -> kvArray.length == 2) .collect(Collectors.toMap(kv -> kv[0], kv -> kv[1])); assertEquals("John", keyValueMap.get("name")); assertEquals("30", keyValueMap.get("age")); assertEquals("NewYork", keyValueMap.get("city")); }
🌐
YouTube
youtube.com › luke chaffey
split string and store it into HashMap java 8 - YouTube
java: split string and store it into HashMap java 8Thanks for taking the time to learn more. In this video I'll go through your question, provide various ans...
Published: November 22, 2022
Views: 90
🌐
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.
🌐
Java2s
java2s.com › example › android › java.util › split-string-into-map.html
split string Into Map - Android java.util
split string Into Map · import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main{ public static Map<String, String> splitIntoMap(String text, String delimiter, String separator) { HashMap<String, String> ret = new HashMap<String, String>(); String decodedString = ""; try {/*from w w w. ja va2 s.c o m*/ decodedString = URLDecoder.decode(text, "UTF-8"); } catch (Exception e) { } String[] split = decodedString.split(delimiter); for (String string : split) { if (string.contains(separator)) { String[] element = string.split(separator); if (element.length == 2) ret.put(element[0], element[1]); } } return ret; } } Previous ·
Find elsewhere
🌐
Metamug
metamug.com › article › java › string › java-split-lines-hashmap.html
Java Split Lines to Hashmap
Java 11 provides String.lines() method to split the string into lines. Traditionally, developers needed to split the lines using line separator regex. Stream<String> stream = string.lines(); The lines() method returns a Stream of lines. Here we have split the line using the equals operator and used collect function to create the hashmap · stream.map(line-> line.split("=")).collect(Collectors.toMap(a->a[0],a->a[1])); Example: name=John Doe age=32 place=New York ·
🌐
KapreSoft
kapresoft.com › java › 2023 › 05 › 03 › java-string-to-map.html
Java • Converting Strings To Map | KapreSoft
May 3, 2023 - To perform this conversion, we can split the input string into an array of key-value pairs using the delimiter, then split each key-value pair into an array of key and value using another delimiter, such as the equals sign.
🌐
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 method, we iterate through the String array and split each element into key and value. Next, the resulting key-value pairs are added to the Map.
🌐
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); } }
🌐
javaspring
javaspring.net › blog › split-string-and-store-it-into-hashmap-java-8
Java 8: Split String and Store in HashMap - Fix Stream Collector Issues with Missing Values — javaspring.net
Java 8 features: Streams, Lambdas, and Collectors. String manipulation (e.g., split() method). Goal: Split a input string into key-value pairs and store them in a HashMap<String, String>.
🌐
DevDreamz
devdreamz.com › question › 810989-java-8-split-string-and-create-map-inside-map
Java 8 Split String and Create Map inside Map - DevDreamz
java-8javasplitjava-stream · You ... this: Map<String, Map<String, String>> result = Arrays.stream(samp.split(",")) .map(i -> i.split("-")) .collect(Collectors.groupingBy(a -> a[0], Collectors.toMap(a -> a[1], a -> a[2]))); First it creates ...