0

This might be a very basic question but I'm not used to work with Java and I would like to create an array / list like this:

6546:{
    "Ram":{
         24M,
         4M,
         64M,
         ...
    },

    "Cpu":{
         2%,
         4%,
         6%,
         ...
    },
    ...
}

I've been trying it with LinkedList and so on but end up creating lists of lists and it starts looking very ugly.

This is a very common array in JSON, PHP or even Javascript, what would be the best way to create it by using Java?

Alvaro
  • 40,778
  • 30
  • 164
  • 336

5 Answers5

1

You want a List<List<Integer>> or an int[][].

List<List<Integer>> list = new ArrayList<>();
list.add(new ArrayList<>());
list.get(0).add(24);

But perhaps you just want to use something like Gson and store this as JSON.

Or create a class like:

class Data {
    private final List<Integer> ram = new ArrayList<>();
    private final List<Integer> cpu = new ArrayList<>();
}

Or if you want to avoid creating classes? (Which you shouldn't)

Map<String, List<Integer>> map = new HashMap<>();
map.put("cpu", new ArrayList<>());
map.put("ram", new ArrayList<>());
djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Could you be more concrete? (and they are strings) – Alvaro Feb 27 '14 at 17:39
  • collection inside collection? bad practice – solvator Feb 27 '14 at 17:40
  • @solvator Why? Can you say something more or provide resources where we could read more about it? – Pshemo Feb 27 '14 at 17:40
  • 1
    bad practice? maybe. but not always avoidable – Sean Patrick Floyd Feb 27 '14 at 17:40
  • 1
    @solvator that's false and I don't know where you get that idea. – djechlin Feb 27 '14 at 17:41
  • @djechlin one more map is needed, the one with the number as key. Which is a map inside a map again... – Alvaro Feb 27 '14 at 17:47
  • It doesn't really solve my problem properly, as becomes very complex if you have more than 2 or 3 nested levels... but well, I guess Java is not that flexible with arrays of data as PHP or JSON. – Alvaro Feb 27 '14 at 17:57
  • If you stick with primitive or simple types like Map and List, it's actually not much more complex than PHP or Python. The difference is largely in the tools available to interact with these structures. In Python/PHP/Perl, the focus is more on speed of development and ease of interaction, and their softly typed nature lends well to this approach. Java isn't like that, as you have to pay careful attention to types, and as such the more flexible structure interactions you are used to aren't available in Java. The same data structures are available, and likely are similarly complex under the hood – ProgrammerDan Feb 27 '14 at 23:17
0

Array of array you can define like - String[][].

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

It might be done in that way.

int[][] twoDimTable = new int[size][size];
String[][] twoDimTable = new String[size][size];

or
List<List<Integer> list = new ArrayList<>(); //or
List<List<String> list = new ArrayList<>();
RMachnik
  • 3,598
  • 1
  • 34
  • 51
0

HashMap<Integer, HashMap<String, List<Object>>> looks good.

dimo414
  • 47,227
  • 18
  • 148
  • 244
Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
0

This looks more like a key/value indexed structure.

One way (of many) to do something equivalent in Java:

Map<Integer, Map<String, String[]>> myData = new Hashtable<Integer, Map<String, String[]>>();
Map<String, String[]> entries = new Hashtable<String, String[]>();

entries.put("Ram", new String[] {"24M", "4M"}); // etc.
entries.put("Cpu", new String[] {"2%", "4%"}); // etc.

myData.put(6546, entries);

This would create an equivalent data structure, and you could index into it in a familiar fashion:

myData.get(6546).get("Ram")[0];

Although that would be VERY bad form, as you should always check for nulls before using the results of .get(x), such as:

Map<String, String[]> gotEntry = myData.get(6546);
if (gotEntry != null) {
    String[] dataPoints = gotEntry.get("Ram");

    if (dataPoints != null && dataPoints.length > 0) {
        String data = dataPoints[0];
     }
}

And so on. Hope this helps!

One other more interesting option is to use something like described here where you can define your data as a JSON string, and convert it into Object types later using un/marshalling.

Community
  • 1
  • 1
ProgrammerDan
  • 871
  • 7
  • 17