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?

6
  • That isn't valid JSON, what sort of data structure are you trying to represent? Commented Feb 27, 2014 at 17:39
  • @dimo414 it is just pseudo code :) Commented Feb 27, 2014 at 17:39
  • It doesn't get much better than lists of lists in Java, sorry Commented Feb 27, 2014 at 17:40
  • Java is an OO language. Create your own classes instead of trying to create List of lists of maps of lists. Commented Feb 27, 2014 at 17:42
  • @SeanPatrickFloyd fundamentally disagree, but you need to know what structure you're trying to represent. You can use Lists, Maps, and custom objects, same as any other language. Commented Feb 27, 2014 at 17:42

5 Answers 5

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<>());
Sign up to request clarification or add additional context in comments.

8 Comments

Could you be more concrete? (and they are strings)
collection inside collection? bad practice
@solvator Why? Can you say something more or provide resources where we could read more about it?
bad practice? maybe. but not always avoidable
@solvator that's false and I don't know where you get that idea.
|
0

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

Comments

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<>();

Comments

0

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

1 Comment

No it doesn't. It looks like an abomination. But sometimes that's what you have to work with.
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.

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.