Search code examples
javacollectionskey-value

Best collection to use for small number of key-value pairs in Java


I'm currently creating a prototype of a Rock-Paper-Scissors program and I need to store the selections in an Integer-String format.

What would be the "best" collection to use, in terms of speed of searching and memory usage?

The premise being that the computer will pick a random number, then the key-value pairs are searched to find the appropriate selection name for use later in the program.

EDIT:
To clarify, there will be at most 5 key-value pairs in the collection, with Integers as the keys.


Solution

  • An ArrayList or an HashMap would be fine if your keys are Integer.

    Both are O(1) and expect unique keys.

    Otherwise, if they are String, HashMap only would fit.