Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java: How to convert HashMap to ArrayList?

Hi in my program I was using HashMap to store some values of the type String and Object. I was hoping to convert this to an ArrayList so I wouldn't need to use a HashMap at all. Here's the snippets of my code that has HashMap in it:

private HashMap directions;

descriptions = new HashMap();

And then later, inside a method and in some if loops, I have:

directions.put("north", north);

directions.put("east", east);

directions.put("south", south);

directions.put("west", west);

And later I have:

Set keys = directions.keySet();

Is it possible to do this using an ArrayList instead?

3 Answers

Relevance
  • 7 years ago

    Why are you using HashMap() rather than HashMap<K,V>()?

    Doesn't your IDE or compiler warn you that:

    HashMap is a raw type. References to generic type HashMap<K,V> should be parameterized?

    Right now, you are indexing into the hashmap using strings: north, south, etc.

    For an ArrayList, you can only index by an integer, through the get method.

    You can do list.get(1), but you can't do list.get("north") with an ArrayList. (You also can't do list[1], but that is just because Java is a mildly retarded language that doesn't have proper support for operator overloading).

    What you can do is use an Enum:

    public enum Direction { NORTH, SOUTH, EAST, WEST };

    That will basically give you 4 symbolic names Direction.NORTH, Direction.SOUTH, etc. With values 0, 1 ,2 and 3. Since they are really just numbers, you can use them to index into an array:

    directions.get(Direction.NORTH);

    Oh...wait...that won't work. Good grief Java is just plain too pedantic sometimes.

    This should work:

    directions.get(Direction.NORTH.ordinal());

  • 6 years ago

    This Site Might Help You.

    RE:

    Java: How to convert HashMap to ArrayList?

    Hi in my program I was using HashMap to store some values of the type String and Object. I was hoping to convert this to an ArrayList so I wouldn&#39;t need to use a HashMap at all. Here&#39;s the snippets of my code that has HashMap in it:

    private HashMap directions;

    descriptions = new...

    Source(s): java convert hashmap arraylist: https://shortly.im/uSJIs
  • Bob
    Lv 7
    7 years ago

    The problem is that you've already stumbled upon the best solution for storing your data (a HashMap) so it's somewhat perverse to change it to an inferior structure.

    I guess the underlying question is why do want to lose the HashMap? It's a standard part of Java, it's fairly efficient and it's really useful. So why not use it?

    PS: You should use the generic version "HashMap<String, Object>() " rather than the raw version "HashMap()" which is now obsolete.

Still have questions? Get your answers by asking now.