java - 处理 HashMap<Integer, HashMap<String, Integer>> 以匹配 HashMap<String, Integer> 的字符串

标签 java

我有一个数据库表,其中包含电视节目类型列表和关联的 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<String, Integer> 获取实际的字符串(键)为了检查分隔的“流派”字符串是否包含它并使用它来获取流派颜色的 ARGB 整数?难道我的想法都是错的吗?

最佳答案

您的流派 map 可以包含多种流派,因此您必须迭代键:

    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字符串包含多个键(例如Film;Comedy),您应该考虑如果genre您希望做什么code>map 包含其中多个。您是否返回找到的第一个匹配项的argbColor

关于java - 处理 HashMap<Integer, HashMap<String, Integer>> 以匹配 HashMap<String, Integer> 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25235016/

相关文章:

java - JPA 选择日期低于参数的实体

java - 将初始化参数添加到 init(ServletConfig) 中的列表

java - 查找具有不同长度的行的二维数组中的列之和

java - 为什么 YARN java 堆空间内存错误?

java - 拉绳 linux java

Java:原始类型与泛型

java - 从 application.properties : Attribute value must be constant 读取

java - 连接表丢失

java - 使用嵌套 TextView 限制 Android Horizo​​ntal ScrollView 的宽度

java - Java 中的 "A Map of Class<?> to List<The class>"怎么说?