Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
596 B
3
Indexable
Never
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));
        }
    }
}
Leave a Comment