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
🌐
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 - After running this code, we will have a Map containing the key-value pairs extracted from the input string. We can also use the String.split() method to convert a string to a map in Java.
🌐
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.
🌐
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")); }
🌐
Medium
parathan.medium.com › split-map-a-string-in-java-bdb006ef45f9
Split & Map a String in Java
September 27, 2018 - Inside that if else is used to ... using Map, Arrays in Java. Splitting the string by “ ; ” and “ = ” and assigning the key to the o th index of Map & value to the 1st index of Map...
🌐
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; ...
🌐
Stack Overflow
stackoverflow.com › questions › 27305114 › split-a-string-to-a-map
java - Split a string to a map - Stack Overflow
import java.util.*; import java.util.regex.*; public class map { public static void main(String[] argv) { String string = "...\nfield [\nname = marked\nvalue = false\n]\n" + "field [\nname = currenFolder\nvalue = null\n]\n...\n"; Map<String, String> map = new HashMap<String, String>(); Pattern p = Pattern.compile("field .(?s).name = (.*?).value = (.*?).]"); Matcher m = p.matcher(string); while (m.find()) map.put(m.group(1), m.group(2)); System.out.println(map); } }
Find elsewhere
🌐
Java2s
java2s.com › example › android › java.util › split-string-into-map.html
split string Into Map - Android java.util
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 ·
🌐
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); } }
🌐
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 ·
🌐
Baeldung
baeldung.com › home › java › java streams › string operations with java streams
String Operations with Java and Stream API | Baeldung
January 8, 2024 - Java 8 has introduced a new Stream API that lets us process data in a declarative manner. In this quick article, we would learn how to use the Stream API to split a comma-separated String into a list of Strings and how to join a String array into a comma-separated String. We’ll also look at how to convert a string array to map using Stream API.
🌐
KapreSoft
kapresoft.com › java › 2023 › 05 › 03 › java-string-to-map.html
Java • Converting Strings To Map | KapreSoft
May 3, 2023 - In this example, we first split the input string into an array of key-value pairs using the semi-colon delimiter. Then, we use the map() method to split each pair into an array of key and value using the equals sign delimiter.