I'm working with this data structure in PHP
Array
(
[leigh@bla] => Array
(
[chat1] => Array
(
[0] => Array
(
[0] => 0
[1] => hi
[2] => 123213312
)
)
[chat2] => Array
(
[0] => Array
(
[0] => 0
[1] => whatever?
[2] => 123213312
)
[1] => Array
(
[0] => 1
[1] => ok
[2] => 23123213
)
)
)
[nelson@bla] => Array
(
[chat1] => Array
(
[0] => Array
(
[0] => 1
[1] => hello
[2] => 1232132123
)
)
)
)
Here is the PHP code:
<?php
$arrTemp['leigh@bla']['chat1'][] = array(0, 'hi', '123213312');
$arrTemp['leigh@bla']['chat2'][] = array(0, 'whatever?', '123213312');
$arrTemp['leigh@bla']['chat2'][] = array(1, 'ok', '23123213');
$arrTemp['nelson@bla'] = array('chat1'=>array(array(1, 'hello', '1232132123')));
echo '<pre>';
print_r($arrTemp);
I'm trying to store this structure in Java. But struggling to find a suitable type, i've tried ArrayList> etc. What would be the best way of storing this structure in Java?
-
Is this for a kind of experiment or real code?Luiggi Mendoza– Luiggi Mendoza2013年07月18日 15:42:54 +00:00Commented Jul 18, 2013 at 15:42
-
1Think in terms of objects. Not in terms of data structures. What does this PHP data structure represent?JB Nizet– JB Nizet2013年07月18日 15:44:20 +00:00Commented Jul 18, 2013 at 15:44
-
I'm looking at porting some PHP code into Java, the PHP code in this question just mocks up what is actually created for simplicity. It represents chat data for Microsoft OCS serverAndy Davies– Andy Davies2013年07月18日 15:44:29 +00:00Commented Jul 18, 2013 at 15:44
-
The array directly underneath of the "leigh" part starts with the index 1 instead of 0 - ist that important?f1sh– f1sh2013年07月18日 15:57:28 +00:00Commented Jul 18, 2013 at 15:57
-
I've updated the PHP and debug output to make it more obvious about the 2nd key. Leigh could contain many chats which contain the inner structure of flag, message, timestampAndy Davies– Andy Davies2013年07月18日 16:07:16 +00:00Commented Jul 18, 2013 at 16:07
1 Answer 1
The outer structure seems to be an associative array (the key is "leigh"), so for that level you need a (Hash)Map (there are no associative arrays in java, a Map is the equivalent).
Inside of that seems to be a list of lists and inside of that some structure that holds 3 atomic values (let's call them a, b and c) that look like Integer, String, Integer.
A simple container for those 3 values would look like this:
class InnerStructure {
Integer a, c;
String b;
}
and to represent your structure:
Map<String, List<List<InnerStructure>>> wholeStructure;
Note that Map and List are only interfaces, implementations for those are HashMap and LinkedList or ArrayList.
Imitating your php code to fill this structure:
Map<String, List<List<InnerStructure>>> wholeStructure = new HashMap<String, List<List<InnerStructure>>>();
List<List<InnerStructure>> outerList = new ArrayList<List<InnerStructure>>();
List<InnerStructure> innerList = new ArrayList<InnerStructure>();
InnerStructure innerData = new InnerStructure();
innerData.a = 1;
innerData.b = "hello";
innerData.c = 1232132123;
innerList.add(innerData);
outerList.add(innerList);
wholeStructure.put("leigh", outerList);
Of course i strongly recommend that you don't simply work with nameless lists, but instead find names or terms that describe the acutal use for this hierarchy. Then you create classes for those and aggregate the lists into these classes, which makes it way more readable, typesafe and less painful to inspect.