0

过去 3 天我花时间让以下结构工作,但我无法弄清楚如何让结构工作

array
  1 => 
    array
      0 => 
        array
          'id' => '1'
          'name' => 'xyz'
      1 => 
        array
          'id' => '12'
          'name' => 'xyz1'
      2 => 
        array
          'id' => '54'
          'name' => 'xyz12'
  20 => 
    array
      0 => 
        array
          'id' => '1'
          'name' => 'xyz'
      1 => 
        array
          'id' => '12'
          'name' => 'xyz1'
      2 => 
        array
          'id' => '54'
          'name' => 'xyz12'
      3 => 
        array
          'id' => '566'
          'name' => 'xyz1234'

我尝试了以下事情,但我无法继续前进

Map<Integer, ArrayList<HashMap<String, Object>>> data  = new HashMap<Integer, ArrayList<HashMap<String, Object>>>();

我有一个结果集,因为我有如下信息

id               | name                      | element_id
______________________________________________________________
1                | xyz                       | 1
______________________________________________________________
1                | xyz                       | 3
______________________________________________________________
12               | xyz1                      | 1
______________________________________________________________
54               | xyz11                     | 1
______________________________________________________________
566              | xyz1234                   | 3
______________________________________________________________
12               | xyz1                      | 3
______________________________________________________________
54               | xyz11                     | 3
______________________________________________________________

我的代码是

while (resultSET.next()) 
{
    Map<String, Object> tag = new HashMap<String, Object>();
    tag.put("name", resultSET.getString(3));
    tag.put("id", resultSET.getInt(2));

    tags.put(resultSET.getInt(1), tag);
}
4

4 回答 4

2

在处理集合的集合(集合......)时,事情往往会很快变得混乱。尝试将不同的集合封装在(描述性命名的)对象中,我怀疑它会更容易管理。

于 2012-05-07T19:02:58.283 回答
1

You should try

Map<Integer, Map<Integer, Map<Integer, Integer>>> result = new HashMap<Integer, Map<Integer,Map<Integer,Integer>>>();

Where in the final map you will store the values

'id' => '1'
          'name' => 'xyz'

and for the second level map you should store

0 => 
        array
          'id' => '1'
          'name' => 'xyz'

And for the final outer map you should store

1 => 
    array
      0 => 
        array
          'id' => '1'
          'name' => 'xyz'
      1 => 
        array
          'id' => '12'
          'name' => 'xyz1'
      2 => 
        array
          'id' => '54'
          'name' => 'xyz12'

Hope this works for you

Enjoy !!!

于 2012-05-07T18:59:17.840 回答
0
Map<String, Object> tag = new HashMap<String, Object>();
tag.put("name", resultSET.getString(3));
tag.put("id", resultSET.getInt(2));

您可以做的一个明显的简化是使标记成为具有属性名称和 ID 的类。有什么理由你不想这样做吗?


关于您当前代码的问题:

tags.put(resultSET.getInt(1), tag);

您将需要创建一个包含标签的数组列表,然后将其放入标签中。

于 2012-05-07T19:01:54.507 回答
0

没那么难……!终于明白了……!!

HashMap<Integer, ArrayList<HashMap<String, Object>>> tags  = new HashMap<Integer, ArrayList<HashMap<String, Object>>>();

while (routeRs1.next()) 
{
    ArrayList<HashMap<String, Object>> temp_tags    =   new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> tag     =   new HashMap<String, Object>();

    if(tags.get(routeRs1.getInt(1)) == null)
    {
        tag.put("name", routeRs1.getString(3));
        tag.put("id", routeRs1.getInt(2));

        temp_tags.add(tag);

    }
    else
    {
        temp_tags   =   (ArrayList<HashMap<String, Object>>) tags.get(routeRs1.getInt(1));

        tag.put("name", resultSET.getString(3));
        tag.put("id", resultSET.getInt(2));
        temp_tags.add(tag);
    }
    tags.put(resultSET.getInt(1), temp_tags);
}

简单的软件概念就是分解问题......!:D

于 2012-05-08T05:57:11.490 回答