LinkedHashMap as an array

Posted on 2009-04-20
Medium Priority
Last Modified: 2012-05-06

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?

Question by:DJ_AM_Juicebox
LVL 92

Assisted Solution

objects earned 600 total points
ID: 24190506
should be ok, what result are you getting?


Assisted Solution

aman123_123 earned 600 total points
ID: 24190902
The values the map has are three new apple objects so entries[1] will not have any information for "green".
LVL 11

Accepted Solution

Steiner earned 800 total points
ID: 24194968
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.


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question