LinkedHashMap as an array

Hi,

I have a LinkedHashMap - I think it retains the order in which you add items, kind of like an array. I want to access an element by an index. Something like:

    LinkedHashMap<String, Apple> map = new LinkedHashMap<String, Apple>();
    map.put("red", new Apple());
    map.put("green", new Apple());
    map.put("yellow", new Apple());

    Apple[] entries = (Apple[])(map.values().toArray(new Apple[0]));
    entries[1] ; // green apple.

but that doesn't work - what's the right way to get the linked hashmap entries as an array?

Thanks
DJ_AM_JuiceboxAsked:
Who is Participating?
 
SteinerCommented:
If you want the color and the Apple-Object, you should try the entrySet instead of the values collection. That set consists of Map.Entry-Elements which contain the key AND the value from the map.

Daniel
0
 
objectsCommented:
should be ok, what result are you getting?

0
 
aman123_123Commented:
The values the map has are three new apple objects so entries[1] will not have any information for "green".
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.