I am translating a Javascript function to Java, and I am having troubles with this text variable below, how to declare and use it in Java:
Javascript:
var text={}; // Index will be several arrays, and value an integer
var word; // Will contain several chars, like a string
var myChar; // Will contain only one char
var value; // Will contain one integer
[...]
if(text[word+myChar]!=null)
{ // In this comparison, word is "hell", myChar is "o" and value is 1234
text[word+myChar]=value;
}
Thanks for your help
It would be very helpful if we can see what you have tried so far (in Java). This can help us put into context better what you’re trying to accomplish.
Here are my tips from what I can gather:
HashMap<K,V>in Java. This will say “Map an instance of a value of type V to a key of type K” where K and V can be decided by you. With this, you could create an instance of aHashMap<String, Integer>where you could get/set integer values within the hash map at particular string key.Stringobject from this.HashMap, you may want to usecontainsKey(K key)orget(K key)depending on whether you want to see if the key exists in the map at all, or if the value at that key in the map is non-null. This will depend on the context of the problem and what you want to accomplish.Here’s a quick (not tested) example of what it may look like: