LinkedHashMap as an array

Posted on 2009-04-20
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

    should be ok, what result are you getting?

    LVL 4

    Assisted Solution

    The values the map has are three new apple objects so entries[1] will not have any information for "green".
    LVL 11

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    This video teaches viewers about errors in exception handling.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now