【问题标题】:Process HashMap<Integer, HashMap<String, Integer>> to match the String of the HashMap<String, Integer>处理 HashMap<Integer, HashMap<String, Integer>> 匹配 HashMap<String, Integer> 的字符串
【发布时间】:2014-08-11 02:14:17
【问题描述】:

我有一个数据库表,其中包含电视节目类型列表和关联的 ARGB 颜色值,用于在显示电视指南时突出显示 Android ListView 中的电视节目。流派表如下所示...

id   genre   color
i    Science FF52ADAB
2    Film    FFDC7223

然后我执行以下操作...

Map<Integer, Map<String, Integer>> genreList = new HashMap<Integer, HashMap<String, Integer>>();
// Populate genreList

每个电视节目(从不同的表中检索)都有一个用于流派的分隔字符串,例如 Film;Comedy,所以我正在执行以下操作...

if (genreList != null) {
    int genreCount = genreList.size();
    Map<String, Integer> genre;
    int argbColor;

    // Get the delimited genres field of the TV show, e.g., Film;Comedy
    String genres = cursor.getString(cursor.getColumnIndex("genres"));

    for (int count = 0; count < genreCount; count ++) {
        genre = genreList.get(count);
        genres = cursor.getString(cursor.getColumnIndex("genres"));

        // Now I need to get the key from the genre HashMap
        if (genres.contains(/* genre key here */)) {
            // Now get the ARGB value associated with the genre
            argbColor = genre.get(/* genre key here */);
            return argbColor;
        }
    }
}

那么,我如何从HashMap&lt;String, Integer&gt; 获取实际的字符串(键),以检查分隔的“流派”字符串是否包含它,并使用它来获取流派颜色的 ARGB 整数?我是不是搞错了?

【问题讨论】:

  • 流派只有一个元素吗?假设 id = 2,那么流派哈希仅包含其中的电影?
  • 是的,实际流派表每个流派都有一行。电视节目可以有与之相关的定界流派。

标签: java


【解决方案1】:

您的genre 映射可以包含多种类型,因此您必须遍历键:

    genre = genreList.get(count);
    for (String g : genre.keySet()) {
      if (genres.contains(g)) {
        // Now get the ARGB value associated with the genre
        argbColor = genre.get(g);
        return argbColor;
      }
    }

当然,如果genres String 包含多个键(例如Film;Comedy),您应该考虑如果genre 映射包含多个键,您希望做什么。您是否返回找到的第一个匹配项的 argbColor

【讨论】:

  • 好的,迭代键集可能是我错过的。你是对的,有一个分隔的流派字符串我可能只想匹配第一个,所以我需要使用startsWith(...) 而不是contains(...)
  • 谢谢。稍作修改就解决了我的问题。
【解决方案2】:

您的genre 是一个Map&lt;String, Integer&gt;,它只有一个Entry

您可以使用genre.entrySet() 访问条目的Set 并获取第一个(通常是唯一)。

有了样本数据,与 first 类型相关联的 Entry"Science" 作为 keyFF52ADAB 作为


但是,我建议您使用更明智的结构来表示您的数据...也就是说,我建议创建这样一个类:

public class Row {
    private final String genre;
    private final Integer colour;

    // constructor

    // getters
}

...并且拥有:

Map<Integer, Row> genreList = new HashMap<>();

...您可以按如下方式使用:

genres = cursor.getString(cursor.getColumnIndex("genres"));

for (int count = 0; count < genreCount; count ++) {
    genre = genreList.get(count);

    final String genreName = genre.getGenre();
    final String genreColour = genre.getColour();

    // Now I need to get the key from the genre HashMap
    if (genres.contains(genreName)) {
        // Now get the ARGB value associated with the genre
        return genreColour;
    }
}

【讨论】:

  • 谢谢。我接受了 Eran 的回答,因为它很容易与我当前的代码配合使用。不过你是对的 - 从逻辑上讲,我需要为流派名称和颜色定义一个类,这也是我打算做的事情。不幸的是,我正在使用从未正常工作过的遗留代码,所以我需要先把它弄到一个点才能继续前进。
猜你喜欢
  • 1970-01-01
  • 2015-02-19
  • 2016-06-11
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
相关资源
最近更新 更多