【问题标题】:Can't retrieve Double from HashMap<Integer, Double>无法从 HashMap<Integer, Double> 中检索 Double
【发布时间】:2015-03-16 20:02:34
【问题描述】:

不知何故,我无法从使用 Gson 制作的 HashMap 中检索 Double。

Map<Integer, Double> ratingMap = (Map<Integer, Double>) new GsonBuilder()
            .create().fromJson(json, Map.class);
Integer ifilmId = filmId;
Double rating = ratingMap.get(ifilmId);

在这段代码中,我验证了 ratingMap 包含 {2=5.0},但是当我执行 ratingMap.get(ifilmId)(我验证 ifilmId 实际上是 2)时,变量 rating 为空。我在这里错过了什么吗?

我通过以下方式创建 HashMap:

if (json.equals("")) {
        // noting ever saved
        ratingMap = new HashMap<Integer, Integer>();
        ratingMap.put(filmId, rating);

} else {
        ratingMap = (Map<Integer, Integer>) new GsonBuilder().create()
                .fromJson(json, Map.class);
        ratingMap.put(Integer.valueOf(filmId), rating);
}

我让 Gson 将 Integer 格式化为 Double,这似乎可以正常工作,但我无法检索它。

总代码,包括保存到 Android 的 SharedPreferences

public void saveRating(int rating, int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(
            LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    Map<Integer, Integer> ratingMap;
    if (json.equals("")) {
        // noting ever saved
        ratingMap = new HashMap<Integer, Integer>();
        ratingMap.put(filmId, rating);

    } else {
        ratingMap = (Map<Integer, Integer>) new GsonBuilder().create()
                .fromJson(json, Map.class);
        ratingMap.put(Integer.valueOf(filmId), rating);
    }

    json = new GsonBuilder().create().toJson(ratingMap, Map.class);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(LOCAL_MAP_RATING_KEY, json);
    editor.commit();
}

/*
 * returns null if no rating found
 */
public Map<Integer, Integer> getRatingFor(int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(
            LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    if (json.equals("")) {
        return null;
    }

    Map<Integer, Integer> ratingMap = (Map<Integer, Integer>) new GsonBuilder()
            .create().fromJson(json, Map.class);
    Log.d("map", ratingMap.toString());
    Integer ifilmId = filmId;
    Integer rating = ratingMap.get(ifilmId);
    if(rating == null) { //because of this we have to prevent a 0 rating
        return null;
    } else {
        Map<Integer, Integer> returnMap = new HashMap<Integer, Integer>();
        returnMap.put(filmId, rating.intValue());
        return returnMap;
    }
}

【问题讨论】:

  • 尝试执行map.put(2, 4.0),然后打印map。看看你得到了什么。
  • 您创建了一个Map&lt;Integer, Integer&gt; 并尝试获取一个Map&lt;Integer, Double&gt; ?
  • @AlexisC。是的。 Gson 确实正确格式化了它。当我将检索到的 Map 更改为“Map”时,问题仍然存在。
  • 你说Log.d("map", ratingMap.toString()); 打印{2=5.0} 并且ifilmId 是2,你得到null
  • 你能尝试发布一个小程序来演示这个问题吗?我认为它与SharedPreferences 无关,因此您尝试保存的带有 JSON 的简单 Java 程序就可以了。

标签: java android hashmap gson


【解决方案1】:

确保在保存时不传递空变量

saveRating(int rating, int filmId){
    Log.d(TAG, String.valueOf(rating));
}

if (json.equals("")) {
    // noting ever saved
    ratingMap = new HashMap<Integer, Double>();    <--- Double not Integer
    ratingMap.put(filmId, 5.0);

} else {
    ratingMap = (Map<Integer, Double>) new GsonBuilder().create()
            .fromJson(json, Map.class);           <--- double not Integer
    ratingMap.put(Integer.valueOf(filmId), 5.0);
}

确保在使用双打时
使用 5.0
不是 5

【讨论】:

  • 我尝试过使用 Double.valueOf(rating)。但它没有用。当我的 logcat 确实指示 {2=4.0} 时,检索到的内容一直返回 null
【解决方案2】:
public void saveRating(Double rating, int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    Map<Integer, Double> ratingMap;
    if (json.equals("")) {
        // noting ever saved
        ratingMap = new HashMap<Integer, Double>();
    } else {
        ratingMap = (Map<Integer, Double>) new GsonBuilder().create().fromJson(json, Map.class);
    }
    ratingMap.put(filmId, rating);
    ratingMap.put(3, 5.0d); // JUST FOR TEST
    json = new GsonBuilder().create().toJson(ratingMap, Map.class);
    SharedPreferences.Editor editor = sharedPref.edit();

    editor.putString(LOCAL_MAP_RATING_KEY, json);
    editor.commit();
}

/*
 * returns null if no rating found
 */
public Map<Integer, Double> getRatingFor(int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");

    if (json.equals("")) {
        return null;
    }

    Map<Integer, Double> ratingMap = (Map<Integer, Double>) new GsonBuilder().create().fromJson(json, Map.class);
    Log.d("map", ratingMap.toString());
    Log.d("map", ratingMap.get(3) + ""); // JUST FOR TEST
    Integer ifilmId = filmId;
    Double rating = ratingMap.get(ifilmId);

    if (rating == null) { //because of this we have to prevent a 0 rating
        return null;
    } else {
        Map<Integer, Double> returnMap = new HashMap<Integer, Double>();
        returnMap.put(filmId, rating);
        return returnMap;
    }
}

【讨论】:

  • “你在 HashMap 中改变对象。这样做是被禁止的” 不。你从哪里得到的?
  • 很抱歉,我没有得到答案。我在哪里改变 HashMap?
  • 但我只为一个唯一键调用 .put 一次。之后,我调用 .put on 换一个不同的键。
  • @TimKranen 给我一分钟 :)
  • 我试过这个。问题仍然存在。映射到字符串打印:{2=5.0},并且当使用 .get(filmId) where filmId = 2 时,作为双精度返回的评分为空。
猜你喜欢
  • 1970-01-01
  • 2015-04-30
  • 1970-01-01
  • 2014-12-19
  • 2021-03-29
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多