2

I've got a HashMap<Object, String[]> I just want to grab the 0 index position from the String[]. How do I do that?

Can I just do this? mMap.get(position)[0]?

1
  • Where is the problem in simply trying this? Commented Jun 2, 2011 at 18:08

4 Answers 4

6

Yes, you can do what you've indicated, provided position is a key in the map.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, for understanding the question and providing the correct response that yes, it is valid as long as position is a key.
4

HashMap doesn't have a 0index and it doesn't have a String[]

You cannot do what you ask because it doesn't make sense.

Can I just do this? mMap.get(position)[0]?

You can. Have you tried this to see if it works? Note: it will fail if map.get() returns null

2 Comments

I'm saying i want to get the 0 index postion of the String[] at whatever position in the HashMap..
You can call a variable "position" but HashMap doesn't have positions it has keys and values. Otherwise you should be able to answer your own question by trying it.
2

A map is not an array. It's a dictionary of keys that map to items. So there is no ordered index in the Map part, there's a lookup key. This means that a Map has no "next" item.

In the event that you stored a String[] in the map, then you could get the first element of the String array like so:

String first = ((String[])mMap.get(lookupKey))[0];

Since you are using generics, your compiler will make the casting unnecessary, simplifying the answer to

String first = mMap.get(lookupKey)[0];

Note that this is not using the position to access the item stored in the Map, it's using the lookup key. In addition, there is a casting of the returned Object into a String[] (because we stored a String[] in the map earlier), and then there is a dereferncing of the first ('0') element.

2 Comments

@mre, depends on the version of Java. Pre-generics, yes there was a need to cast. However, the poster just edited the question to include the type of the map which now has generics. Or if he didn't edit it, I need more coffee because I missed it.
No, your coffee level's fine, the OP clarified it within the edit window. ;-)
1

Here is a little demo program that does what you're asking.

import java.util.Map;
import java.util.HashMap;

public class FirstElementInHashMap {

    public static void main(String[] args) {
        Map<Object,String[]> mMap = new HashMap<Object,String[]>();
        mMap.put("myKeyA", new String[] { "myValue1", "myValue2", "myValue3" });
        mMap.put("myKeyB", new String[] { "myValue4", "myValue5", "myValue6" });
        mMap.put("myKeyC", new String[] { "myValue7", "myValue8", "myValue9" });

        Object position = "myKeyB";
        String[] strings = mMap.get(position);

        // make sure position exists in the Map and contains a non-empty array
        // so we don't throw an NullPointerException
        String firstStringInArray = null;
        if (strings != null && strings.length > 0) {
            firstStringInArray = strings[0];
        }
        System.out.println(firstStringInArray);
    }
}

The output of the above program is:

myValue4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.