3

I need a class being HashMap and ArrayList at the same time.

Why do I need HashMap? To get quick access to the object basing on the key.

Why do I need ArrayList? To randomly select one element.

Do you have any idea how to solve this? Is there any ready class from the shelf?

As for now the only solution which comes to my mind is to use ArrayList. Then random access is a piece of cake. And for selecting object basing on the key just using a simple iterator is some kind of solution but far from being perfect....

7
  • Would it suffice to use a single HashMap and the find means of random access? Random()? Commented Nov 4, 2013 at 19:44
  • What is the type of your keys? Commented Nov 4, 2013 at 19:52
  • 3
    This question is far too vague and possibly over-complicated for no real reason. Commented Nov 4, 2013 at 19:58
  • 1
    What's more mysterious is the 4 up-votes this question has.... Commented Nov 4, 2013 at 20:06
  • 1
    If the Map has already been populated, then just dump the values to an ArrayList and use the list for the random selection. No need for a hybrid container IMO. But maybe you can elaborate on exactly what usage scenario you are applying this to. Commented Nov 4, 2013 at 20:16

5 Answers 5

6

You could use composition:

public class HashedList<K, V> {
    private final List<V> list = new ArrayList<>();
    private final Map<K, V> map = new HashMap<>(); 

    protected K getKey(V val);

    public void add(V value) {
        list.add(value);
        map.put(getKey(value), value);
    }

    public V get(int index) {
        return list.get(index);
    }

    public V get(K key) {
        return map.get(key);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Probably something similar seems to be the best idea (two list kept in memory).
0

You can use an array if your key can work as an index for the array. Otherwise a HashMap seems the solution.

3 Comments

Seems the OP wants to both use a non numeric key AND the insertion index. Neither an ArrayList or HashMap solely can answer that, Jeffery's answer seems a correct way to answer both.
@EranMedan - Unless you're a mind reader or having private communications with OP, I do not see any basis for such conclusion.
@Eran as the question is not clear, we can't say which answer is the best fit
0

I would say simple use array. Am i missing something?

8 Comments

I think you're right, unless OP has other requirements (not stated yet).
@PM77-1 very true. If you also analyze map also build on top of array and arraylist also build on top of array.
@PM77-1 array may be an array of element which contains both key and value.
Array of Arrays won't give a fast access by key. Then it's HashMap.
I liked (and still like) your original solution. You got my upVote. I believe that the fact that HasMap is based on array is immaterial here.
|
0

Why do you need both? what is the use case?

If your key is numeric (Integer), then simply use an ArrayList

If your key is not numeric, and your random access is via the non numeric key, use a HashMap

Otherwise, if you need to access by both a (non numeric) key, and the insertion index, you might need to create your own data structure that has both an ArrayList and a HashMap by implementing both Map and List interfaces. (it will have both an ArrayList and a HashMap members, and you'll have to update both on add / remove etc, although some cases will have indecisive definitions, e.g. when removing an item by key, do you shift the other items' indices or not etc...)

If what you want is simply maintaining insertion order, then LinkedHashMap might have been a good built in solution.

The key question is why you need this data structure? this way we might be able to better suggest solutions.

3 Comments

No where in the question did he mention that he wants to maintaing insertion order. Random access is what is desired. Moreover can you explain your data structure?
@Jatin good point, The only time where I needed both a List and a Map was to maintain insertion order, or when I needed something that resembles more a database table structure (access by string key, and by numeric record ID for example). Without knowing the use case, I can't tell which is what the OP meant. Added some clarification about the data structure. In principle it should be more or less like a LinkedHashMap but using an ArrayList instead of a LinkedList. I don't think this is specifically a good data structure, and it might have a lot of edge cases, but it's definitely doable.
+1 for suggesting the idea first. I think you should put some sample code to give this answer more completeness.
-1

Why do I need HashMap? To get quick access to the object basing on the key.

logical reason.

Why do I need ArrayList? To randomly select one element.

Not so strong: Your logic to select an element randomly in an ArrayList can probably also be used on the entrySet of the HashMap...

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.