-2

I'm newbie with Java Is it possible to convert a String, or a String[] as the keys of an Hashmap? I mean:

String keysToConvert = "key1,key2,key3";

into

HashMap<String, Double> hashmap = new HashMap<String, Double>();
[apply in some way the keys of the string]
hashmap.get("key2");

I know that hashmap.get("key2");has no value in this moment. I just would know if there is a way to load the keys in an HashMap.

Vito
  • 746
  • 2
  • 9
  • 25

2 Answers2

3
hashmap.put("key2", 1.0d);
System.out.println(hashmap.get("key2")); // prints 1.0

This is the basic usage of a Map

Pshemo
  • 122,468
  • 25
  • 185
  • 269
michael_bitard
  • 3,613
  • 1
  • 24
  • 35
1

If you have String[], you can add them using a for-each loop:

String[] keys = {"key1", "key2", "key3"};
HashMap<String, Double> hashmap = new HashMap<String, Double>();

for (String key : keys) {
    hashmap.put(key, null);
}
peterremec
  • 488
  • 1
  • 15
  • 46