Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be a good data structure to store both Integer and array[String] values?

I need a data structure to store a number of integer values associated with an array of strings of variable size. For example, this is what I have-

ADDRESS   INSTRUCTION
64        ADD R1, R0, R0 
68        BGTZ R2, #32
.         .
.         .
.         .
124       J #116

where I want to fetch an INSTRUCTION which I suppose should be stored as an array of strings(exclude the comma and '#') based on the ADDRESS value, more like a key-value pair. This is what I think should be a good idea to store it OR if you can suggest me a better approach of representing this in memory it would be great. (I am using Java to code.)

A good data structure(WHICH?) with reasoning(WHY?) and a detailed explanation(HOW?) would be really nice.

like image 894
Prince Avatar asked Dec 16 '22 03:12

Prince


1 Answers

WHICH?

Map<Integer, List<String>>

WHY?

  • it works
  • it's in JDK
  • it's already used by millions around us

HOW?

Map<Integer, List<String>> map = new HashMap<>();
map.put(64, new ArrayList<String>());
map.get(64).addAll("ADD", "R1", "R0", "R0");

PROFIT!

like image 128
Sergey Grinev Avatar answered Dec 18 '22 16:12

Sergey Grinev