Probably the safest approach is to define a regex pattern for a key/value pair, and then iterate over your string, applying that pattern. In this, case we can try using the following pattern:
\b([^\s]+)=([^\s]+)\b
The key and value will be made available as the first and second capture group, respectively. Some slight finessing was required, because not all of your string is key values (i.e. the leading content does not belong in the final map).
Map<String, String> map = new HashMap<>();
String str="ABCD 1 key1=value1 key2=value2 key3=value3";
String pattern = "\\b([^\\s]+)=([^\\s]+)\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
while (m.find()) {
System.out.println("Found a key/value: (" + m.group(1) + ", " + m.group(2) + ")");
map.put(m.group(1), m.group(2));
}
The above code snippet outputs the following:
Found a key/value: (key1, value1)
Found a key/value: (key2, value2)
Found a key/value: (key3, value3)
Demo