PHP数组结构的Java等价

时间:2012-08-14 11:58:54

标签: java php data-structures

  

可能重复:
  Java associative-array

我在PHP中有一个数组,我需要一种方法来描述Java中的相同类型的数组。什么是Java的等价?

<?php
$data = array(
    "Artist 1" => array(
        "Album 1" => array(
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year")
        ),
        "Album 2" => array(
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year")
        )
    ),
    "Artist 2" => array(
        "Album 1" => array(
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year")
        )
    )
);
?>

1 个答案:

答案 0 :(得分:3)

使用更新的示例我将使用

List<Record> records = ...
records.add(new Record("Artist 1", "Album 1", "id", "title", "genre", "length", "year"));
// etc

之后我会创建所需的索引,甚至使用像MySQL这样的数据库或从普通的CSV加载数据

Artist 1,Album 1,id,title,genre,length,year
Artist 1,Album 1,id,title,genre,length,year
Artist 1,Album 2,id,title,genre,length,year
Artist 1,Album 2,id,title,genre,length,year
Artist 2,Album 1,id,title,genre,length,year

除非有充分理由嵌套数据,否则我会使用复合键

Map<String, List<String>> map = new LinkedHashMap<>();
map.put("1,1", Arrays.asList("abcdefgh", "abcdefgh", "abcdefgh"));
map.put("1,2", Arrays.asList("abcdefgh", "abcdefgh", "abcdefgh"));
map.put("2,1", Arrays.asList("abcdefgh", "abcdefgh"));

Java,作为面向对象的语言,最好使用Objects来尽可能地存储数据结构。