forum

Home / DeveloperSection / Forums / How to convert HashMap keys from String to int in java

How to convert HashMap keys from String to int in java

Tom Cruser244204-Feb-2015

I have a HashMap and a properties file that holds key/values. Properties file stores keys/values in this format "4,5=2" I have built a method that loads properties from the file and it puts the pair of "keys/value" into a HashMap Array(String, Integer). But my problem is that I want each element of keys to be stored as an int, in order to use them as parameters to another method. Keys are stored as String. Any help would be appreciated. Thank you!

public static HashMap<String, Integer> hashMap = new HashMap<>();

 

prop.load(input);

Enumeration<?> e = prop.propertyNames();

    while (e.hasMoreElements()) {

           key = (String) e.nextElement();

           intValue=Integer.parseInt(prop.getProperty(key));

           hashMap.put(key, intValue);


Updated on 04-Jul-2023

Can you answer this question?


Answer

2 Answers

Liked By