问题
Apologies for the rather basic question but I am having a little trouble with my code.
I intended to create a database of events and I wanted the flexibility of retrieving an entire day's worth of events, or a month's worth or even a years worth. So I decided to store the data under 'year'>'month'>'day'. Initially I tried to do it using custom java objects that I thought would be easier to work with, but unfortunately this adds an extra child node between each object with the name of the getter method.
I achieved what I wanted using long nested hash maps:
Map<String,Map<String,Map<String,Map<String,Object>>>> mMap = new HashMap<>();
but I am sure that there must be a better way.
I have saved a picture but I can't post it as my reputation <10. link to json map showing what my custom objects made vs the intended result achieve by the nested maps
so here is the class for the first map (object class not included as it is a bit long):
public class DayKeyBookingsMap {
private Map<String, BookingObject> dayKeyBookingsMap = new HashMap<>();
public DayKeyBookingsMap(){}
public DayKeyBookingsMap(BookingObject bookingObject){
dayKeyBookingsMap.put(bookingObject.getDateKey(),bookingObject);
}
public Map<String, BookingObject> getDayKeyBookingsMap() {
return dayKeyBookingsMap; }
public void setDayKeyBookingsMap(Map<String, BookingObject> dayKeyBookingsMap) {
this.dayKeyBookingsMap = dayKeyBookingsMap; }
}
and a separate class of the next level:
public class MonthKeyBookingsMap {
private Map<String,DayKeyBookingsMap> monthKeyBookingsMap = new HashMap<>();
public MonthKeyBookingsMap(){}
public MonthKeyBookingsMap(BookingObject bookingObject){
DayKeyBookingsMap dayKeyBookingsMap = new DayKeyBookingsMap(bookingObject);
monthKeyBookingsMap.put(bookingObject.getMonthKey(),dayKeyBookingsMap);
}
public Map<String, DayKeyBookingsMap> getMonthKeyBookingsMap() {
return monthKeyBookingsMap;
}
public void setMonthKeyBookingsMap(Map<String, DayKeyBookingsMap> monthKeyBookingsMap) {
this.monthKeyBookingsMap = monthKeyBookingsMap;
}
}
and I haven't included the third level. But I constructed the final object with:
YearKeyBookingsMap(myBasicObject)
which called the third level constructure, which in turn used the second level constructor etc.:
public YearKeyBookingsMap(BookingObject bookingObject){
MonthKeyBookingsMap monthKeyBookingsMap = new MonthKeyBookingsMap(bookingObject);
yearKeyBookingsMap.put(bookingObject.getYearKey(),monthKeyBookingsMap);
}
回答1:
To achieve something like this:
You only need to use six lines of code:
Map<String, Object> map = new HashMap<String, Object>();
map.put("dateKey", "20170105");
map.put("eventTitle", "New Booking");
map.put("monthKey", "01");
map.put("yearKey", "2017");
FirebaseDatabase.getInstance().getReference().child("AAAAB").child("2017").child("01").child("20170105").setValue(map);
There is no need to use nested HashMap.
回答2:
This does not help or teach anything about how firebase works...
Map<String, Object> map = new HashMap<String, Object>();
map.put("dateKey", "20170105");
map.put("eventTitle", "New Booking");
map.put("monthKey", "01");
map.put("yearKey", "2017");
FirebaseDatabase.getInstance().getReference().child("AAAAB").child("2017").child("01").child("20170105").setValue(map);
You could fix your data structure, having the year be a child/key isn't as helpful. If you were doing it for future indexing or grouping you would get same result by accessing the value in dateKey. From here all you would have to do is:
mRef = FirebaseDatabase.getInstance().getReference().child(userID);
mRef.addValueEventListnere (blah blah){
for (DataSnapShot snapShot : dataSnapShot.getChildren())
UniqueData currData = snapShot.getValue(UniqueData.class)
currData.getDateKey();
}
Where UniqueData class is nothing more than a constructor and setters/getters
来源:https://stackoverflow.com/questions/47670955/nested-maps-in-firebase-and-custom-objects