java - 如何在java中为嵌套地图创建模型?
问题描述
我正在我的 Android 应用程序中使用 Firestore,并且我正在使用模型来处理 Firestore 的数据。但我想减少对小数据集的 Firestore 读取。在网上搜索后,我发现 Firestore 中的嵌套地图是减少小型数据集读取的更好方法。
但问题是如何为嵌套地图创建模型(有时也需要嵌套 4 次)?下面是我想如何创建嵌套地图的一些示例代码。
String[] backlogs = {"subject1", "subject2", "subject3"};
List<String> logs = Arrays.asList(backlogs);
Map<String, Object> student = new HashMap<>();
student.put("name", STUDENT_NAME);
student.put("id", STUDENT_ID);
student.put("backlogs", logs);
Map<String, Object> sectionA = new HashMap<>();
sectionA.put("students", student);
Map<String, Object> college = new HashMap<>();
college.put("sections", sectionA);
有什么建议请..
解决方案
以前我也为此苦苦挣扎。经过多次尝试,我找到了这个解决方案。试试这个来获取嵌套地图。
public class College {
String name;
Section section;
public College() {
}
public College(String name, Section section) {
this.name = name;
this.section = section;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Section getSection() {
return section;
}
public void setSection(Section section) {
this.section = section;
}
static class Section{
String Section;
Student student;
public Section() {
}
public Section(String section, Student student) {
Section = section;
this.student = student;
}
public String getSection() {
return Section;
}
public void setSection(String section) {
Section = section;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
static class Student{
String name;
String id;
public Student() {
}
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我认为这就是你所需要的。
这将为您提供嵌套地图。如果需要,更改上面的代码以添加列表。
推荐阅读
- python - forwardMessage 方法,只需要一些建议
- discord - 当用户加入服务器时,如何让我的机器人分配一个角色,然后在两分钟后将其移除?
- ios - Swift - 按值对 Array 的元素进行分组(2 乘 2、3 乘 3 等...)
- java - 基于人员类参数的优先队列
- excel - 运行时错误 424:需要对象。我错过了什么?
- asp.net - Asp.net core 2.1 Web App 无法在 IIS 上正确呈现
- javascript - scrollIntoView 函数不会滚动到任何地方
- reactjs - React:编译失败;index.js
- javascript - JavaScript 中的动画问题
- angularjs - angular 7 *ngFor 嵌套数组迭代