java - 从 ArrayList<HashMap<String, String>>> 获取键到另一个数组

标签 java android arrays string arraylist

我有一个 ArrayList>>,它包含某些键值条目。喜欢:-

ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("NewId", newId);
map.put("Title", title);
map.put("Description", description);
myList.add(map);

多个条目的“NewId”可以相似。

我还有一个颜色数组:-

String[] colors = new String[]{"#1F1A17", "#62934D", "#F9B03F", "#7959BC", "#74B8DE", "#E65641", "#7CC8BB", "#D7CE5D", "#D6BE95", "#B694D1"};

我想要一个包含所有具有相同“NewId”的条目的新组,并为它们分配第一种颜色,其他具有下一个相似“NewId”的条目使用第二种颜色,依此类推,直到获得前 10 个相同“NewId”的条目分配了各自的颜色。

例如:- 分组前

NewId  Title  Description
 101   title1  des1
 102   title2  des2
 103   title3  des3 
 101   title4  des4
 102   title5  des5
 103   title6  des6 

分组后,

NewId  Title  Description
 101   title1  des1  ------> color1
 101   title4  des4  ------> color1
 102   title2  des2  ------> color2
 102   title5  des5  ------> color2
 103   title3  des3  ------> color3
 103   title6  des6  ------> color3

我还做了什么:-

public class MyList {

    private ArrayList<HashMap<String, String>> list = new ArrayList<>();

    public boolean add(HashMap<String, String> map) {
        return list.add(map);
    }

    public void setColor(String newId, String color) {
        for (HashMap<String, String> m : list)
            if (m.containsKey(newId))
                m.put("color", color);
    }

    public String getGroupKey(String key, int i) {      
        ArrayList<String> uniqeList = getUniqKeyList(key);
        Collections.sort(uniqeList);
        return uniqeList.get(i);
    }

    public ArrayList<String> getUniqKeyList(String key){
        ArrayList<String> l = new ArrayList<>();
        for (HashMap<String, String> m : list)
            if(!l.contains(m.get(key)))
                l.add(m.get(key));
        return l;
    }
}

public static void main(String[] args) throws Exception {
        MyList myList = new MyList();
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("NewId", newId);
        map.put("Title", title);
        map.put("Description", description);
        myList.add(map);

        String[] colors = new String[]{"#1F1A17", "#62934D","#B694D1"};

        int i=0;
        while (true) {
                    if(i == colors.length) 
                            break;
            String s =  myList.getGroupKey("NewId", i);
            if(s == null)
                break;
            else 
                myList.setColor(s, colors[i++]);
        }       
    }
itemsAdapter = new LazyAdapter(myContext, myList);

但是我得到一个错误:-

 `E/AndroidRuntime(10276): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1`

我该如何解决这个问题?

最佳答案

如果您对异常有疑问,那么您的特定情况下的问题是:

1 此方法始终仅返回 1 元素数组列表:

public ArrayList<String> getUniqKeyList(String key) {
    ArrayList<String> l = new ArrayList<>();
    for (HashMap<String, String> m : list)
        if(!l.contains(m.get(key)))
            l.add(m.get(key));
    return l;
}

2 Colors 数组的长度为 3。

3 当 i=0 时第一次迭代就可以了。

4 第二次迭代 (i=1 < color.length=3) 有问题:

 public String getGroupKey(String key, int i) {      
     ArrayList<String> uniqeList = getUniqKeyList(key);
     Collections.sort(uniqeList);
     return uniqeList.get(i);
 }

uniqeList.get(i) = uniqeList.get(1),但 uniqeList 的长度为 1(从 0 开始计数)。

关于java - 从 ArrayList<HashMap<String, String>>> 获取键到另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23241098/

相关文章:

java - Spring实体中的@Embedded列

java - Mockito:无法模拟静态和非静态方法

android - 从 WebView 捕获导航事件

javascript - 有人可以解释一下向对象添加新属性的循环过程吗?

java - 如何在Appium中获取和存储列表数据以及如何点击特定的搜索记录

java - 在 Java 中造成 native 内存泄漏

java - 如何更改 Android 应用程序中的通知设置?

java - 尝试将按钮放置在 ListView 下方

ruby - 我如何使用 Ruby 找到数组或字符串中每三个数字的乘积?

python - 从元组列表到数组,第二行成为标题