In the following code I'm using a String array as a HashMap value corresponding to a single key. The get() method of HashMap is returning the whole array. What if I want single value
of array. (Say) I want only 'Lion' corresponding to key 'Animal'.
class HMTest{
public static void main(String[] args){
HashMap<String, String[]> subjects = new HashMap<String, String[]>();
subjects.put("Fruit",new String[] {"mango","orange"});
subjects.put("Animal",new String[] {"Lion","Tiger"});
for(String s:subjects.get("Animal"))
System.out.println(s);
}
}
I also tried replacing above for loop something like this
for(String[] s:subjects.get("Animal"))
System.out.println(s[0]);
But its giving me error.
Anybody please help me out.