Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 表示对象关系的推荐数据结构(对象内部的对象)_Java_Data Structures - Fatal编程技术网

Java 表示对象关系的推荐数据结构(对象内部的对象)

Java 表示对象关系的推荐数据结构(对象内部的对象),java,data-structures,Java,Data Structures,我在创建数据结构时遇到了一个问题,我需要表示一个包含对象的对象,每个对象都包含自己的对象。 我需要表示如下内容: [ { id: "fruit", name: "Fruit", parentId: null, type:[ { id: "apple", name: "Apple", parentId: "supercategory-fruit", type:[

我在创建数据结构时遇到了一个问题,我需要表示一个包含对象的对象,每个对象都包含自己的对象。 我需要表示如下内容:

[
  { id: "fruit",
    name: "Fruit",
    parentId: null,
    type:[
           { id: "apple",
             name: "Apple",
             parentId: "supercategory-fruit",
             type:[
                    { id: "greenApple"
                      name: "Green Apple",
                      parentId: "category-fruit-apple",
                      type: []
                     },
                     { id: "redApple"
                       name: "Red Apple"
                       parentId: "category-fruit-apple",
                       type: []
                     },
                     {pinkApple},
                     {..}
                   ]
            },
            {orange},
            {melon},
            {..}
          ]
  },
  {meat},
  {vegetables},
  {..}
]
public class Obj {
    String id;
    String name;
    Obj parent;
    List<Obj> type;
}
因此,我获得了数据库中的所有内容以及它们之间的关系(通过我的parentId字段)。 我需要找到最好的数据结构来填充所有这些信息,以便将其发送到我的API中并很好地呈现出来

理想情况下,我只想获取每个对象的ID和名称,并创建一个较小的数据结构,但我认为它有点复杂

任何想法都好

多谢各位

我的第一次尝试是创建一个
地图

Map的问题是我(现在意识到)不能将整个对象作为键。因此,我的代码将对象的id作为键返回,即使我将整个对象放入其中。

这样的类如何:

[
  { id: "fruit",
    name: "Fruit",
    parentId: null,
    type:[
           { id: "apple",
             name: "Apple",
             parentId: "supercategory-fruit",
             type:[
                    { id: "greenApple"
                      name: "Green Apple",
                      parentId: "category-fruit-apple",
                      type: []
                     },
                     { id: "redApple"
                       name: "Red Apple"
                       parentId: "category-fruit-apple",
                       type: []
                     },
                     {pinkApple},
                     {..}
                   ]
            },
            {orange},
            {melon},
            {..}
          ]
  },
  {meat},
  {vegetables},
  {..}
]
public class Obj {
    String id;
    String name;
    Obj parent;
    List<Obj> type;
}
公共类Obj{
字符串id;
字符串名;
Obj父母;
列表类型;
}

如果不需要实例,可以将父对象更改为
String

如果需要此对象的公共/最简单表示,请尝试定义一个类,如:

public class Content{
 String id ;  //"fruit"
 String name; // "Fruit"
 String parentId; //   parentId: null
 List<Content> type;  // list of other items.
}
公共类内容{
字符串id;/“水果”
字符串名称;/“水果”
字符串parentId;//parentId:null
列表类型;//其他项目的列表。
}
您可以将整个json放入这个类中:

public class Contents{
 List<Content> items;
}
公共类内容{
清单项目;
}

然后迭代到项。

为什么您认为不能将整个对象作为键?你能给我们看一下你的java代码吗?因为当我尝试它时,它只返回了对象的id。我很想创建一个新的类,但是我想我需要改变很多关于模型的东西(比如JAXBContext或索引字段),虽然还不能完全确定。