---
title: "How to convert HashMap keys from String to int in java"  
description: "How to convert HashMap keys from String to int in java"  
author: "Tom Cruser"  
published: 2015-02-04  
updated: 2023-07-04  
canonical: https://www.mindstick.com/forum/12941/how-to-convert-hashmap-keys-from-string-to-int-in-java  
category: "oops"  
tags: ["java"]  
reading_time: 3 minutes  

---

# How to convert HashMap keys from String to int in java

I have a [HashMap](https://www.mindstick.com/forum/159211/what-are-the-differences-between-a-hashmap-and-a-hashtable-in-java) and a [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) file that holds key/[values](https://www.mindstick.com/forum/327/sum-textbox-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](https://www.mindstick.com/articles/1527/string-split-in-c-sharp), [Integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer)). But my [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is that I want each element of keys to be stored as an int, in order to use them as [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp) to another method. Keys are stored as String. Any help would be appreciated. Thank you!

[public](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) HashMap<String, Integer> hashMap = new HashMap<>();

prop.load(input);

[Enumeration](https://www.mindstick.com/blog/88/enumeration-in-c-sharp)<?> e = prop.propertyNames();

while (e.hasMoreElements()) {

key = (String) e.nextElement();

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

hashMap.put(key, intValue);

## Replies

### Reply by Aryan Kumar

Sure. To convert HashMap keys from string to int in Java, you can use the following steps:

1. Import the necessary libraries.
2. Create a HashMap.
3. Iterate through the HashMap and convert each key from string to int.
4. Print the HashMap with the converted keys.

Here is an example of how to convert HashMap keys from string to int in Java:

Java

```plaintext
import java.util.HashMap;

public class HashMapKeyConversion {

    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);

        // Iterate through the HashMap and convert each key from string to int
        for (String key: map.keySet()) {
            map.put(key, Integer.parseInt(key));
        }

        // Print the HashMap with the converted keys
        for (String key: map.keySet()) {
            System.out.println(key + "=" + map.get(key));
        }
    }
}
```

This code will create a HashMap with three entries: "one" -> 1, "two" -> 2, and "three" -> 3. The code will then iterate through the HashMap and convert each key from string to int. Finally, the code will print the HashMap with the converted keys.

The output of the code will be:

Code snippet

```plaintext
one=1
two=2
three=3
```

### Reply by Anonymous User

As you key seems to be a pair of integers, you need a Pair class to be used as a key. Then just access getFirst() or getSecond() to be used with your other api.

```
public class IntPair {    private final int first;    private final int second;     public IntPair(int first, int second) {        this.first = first;        this.second = second;    }     @Override    public int hashCode() {        int hash = 3;        hash = 89 * hash + this.first;        hash = 89 * hash + this.second;        return hash;    }     @Override    public boolean equals(Object obj) {        if (obj == null) {            return false;        }        if (getClass() != obj.getClass()) {            return false;        }        final IntPair other = (IntPair) obj;        if (this.first != other.first) {            return false;        }        if (this.second != other.second) {            return false;        }        return true;    }     public int getFirst() {        return first;    }     public int getSecond() {        return second;    }}
```


---

Original Source: https://www.mindstick.com/forum/12941/how-to-convert-hashmap-keys-from-string-to-int-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
