Untitled
unknown
plain_text
2 years ago
596 B
10
Indexable
public class StringToMap {
public static void main(String[] args) {
String inputString = "Sea-100|Town-200|City-150";
String[] parts = inputString.split("\\|");
HashMap<String, Integer> keyValueMap = new HashMap<>();
for (String part : parts) {
String[] keyValue = part.split("-");
keyValueMap.put(keyValue[0], Integer.parseInt(keyValue[1]));
}
// Print the resulting map
for (String key : keyValueMap.keySet()) {
System.out.println("array[" + key + "]=" + keyValueMap.get(key));
}
}
}Editor is loading...
Leave a Comment