how to create a model for nested maps in java?

Viewed 182

I'm working with Firestore in my Android app, and I'm using a Model to handle data with Firestore. But I want to decrease my Firestore reads for small data sets. After searching the web I found a nested map in Firestore is the better way to decrease reads for small data sets.

But the problem is how can I create a Model for nested maps(sometimes require to nested for 4times too)? The below is some sample code of how I want to create nested maps.

    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);

Any suggestions please..

1 Answers

Previously me too struggled for this. After a lot of attempts I found this solution. Try this for getting nested maps.

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;
        }
    }

I think this is what you need.

This will give you nested maps. Change above code to add list if you need.

Related