如何在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;
        }
    }

我认为这就是你所需要的。

这将为您提供嵌套地图。如果需要,更改上面的代码以添加列表。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章