Java 如何存储<;HashMap<;MyCompositeKeyClass、ArrayList<;MyClass>&燃气轮机;在共享参考中?

Java 如何存储<;HashMap<;MyCompositeKeyClass、ArrayList<;MyClass>&燃气轮机;在共享参考中?,java,android,hashmap,sharedpreferences,gson,Java,Android,Hashmap,Sharedpreferences,Gson,我希望在SharedReferences中存储一个HashMap,其中MyCompositeKeyClass作为键,ArrayList作为值。我该怎么做 我试过这个: public void saveLessonMap(Context context, HashMap<dayKey, ArrayList<Lesson>> lessonMap){ MapWrapper wrapper = new MapWrapper(lessonMap); String se

我希望在SharedReferences中存储一个HashMap,其中MyCompositeKeyClass作为键,ArrayList作为值。我该怎么做

我试过这个:

 public void saveLessonMap(Context context, HashMap<dayKey, ArrayList<Lesson>> lessonMap){
   MapWrapper wrapper = new MapWrapper(lessonMap);
   String serializedMap = gson.toJson(wrapper);

    appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);

    SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();

    prefsEditor.putString(KEY, serializedMap);
    prefsEditor.commit();


}

 public HashMap<dayKey, ArrayList<Lesson>> getLessonMap(Context context){
    appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);
    String wrapperStr = appSharedPrefs.getString(KEY, "");
    MapWrapper wrapper = gson.fromJson(wrapperStr, MapWrapper.class);
    return wrapper.getMap();
}

JSON:
{“map”:{“com.example.ruslanposhuk.timeline.model”。dayKey@ca81be1c“:[{“b时间”:{“年”:2015,“月”:4,“月日”:7,“小时日”:8,“分钟”:30,“秒”:0},“时间”:{“年”:2015,“月”:4,“月日”:7,“小时日”:9,“分钟”:50,“秒”:0},“姓名”:“星期三1”,“房间”:“114”,“教师姓名”:“约翰·史密斯”},{“b时间”:{“年”:2015,“月”:4,“月日”:7,“小时日”:10,“分钟”:0,“秒”:0},“时间”:{“年”:2015,“月”:4,“月日”:7,“小时日”:11,“分钟”:20,“秒”:0},“名称”:“星期三”2,“房间”:“254”,“教师姓名”:“鲍勃·摩尔”},{“时间”:{“年”:2015,“月”:4,“月日”:7,“小时日”:12,“分钟”:0,“秒”:0},“时间”:{“年”:2015,“月”:4,“月”:7,“时间”:“月”:13分钟:20,“秒”:0},“姓名”:“周三3”,“房间”:“178”,“教师姓名”:“彼得·约翰逊”}],“com.example.ruslanposhuk.timeline.model”。dayKey@28f78912“:[{“b时间”:{“年”:2015,“月”:4,“月日”:7,“小时日”:8,“分钟”:30,“秒”:0},“时间”:{“年”:2015,“月”:4,“月日”:7,“小时日”:9,“分钟”:50,“秒”:0},“姓名”:“星期二”,“房间”:“114”teacherName:“John Smith”},{“B时间”:{“年”:2015,“月”:4,“月”:7,“小时”:10,“分钟”:0,“秒”:0},“时间”]}
ArryList
转换为json数据并存储。使用gson库将ArrayList转换为json字符串格式。

gson gson=
  Gson gson  =
        new GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create();
java.lang.reflect.Type type = new TypeToken <HashMap<dayKey, ArrayList<Lesson>>>(){}.getType();
新建GsonBuilder().enableComplexMapKeySerialization().SetPrettypPrinting().create(); java.lang.reflect.Type=new-TypeToken(){}.getType();
保存:
stringjson=gson.toJson(lessonMap,type);

加载:
HashMap lessonMap=gson.fromJson(json,类型);

  Gson gson  =
        new GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create();
java.lang.reflect.Type type = new TypeToken <HashMap<dayKey, ArrayList<Lesson>>>(){}.getType();