【问题标题】:Get Keys from ArrayList<HashMap<String, String>>> into another array从 ArrayList<HashMap<String, String>>> 获取键到另一个数组
【发布时间】:2025-12-08 00:10:01
【问题描述】:

我有一个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”的项目得到分配了各自的颜色。

eg:- 分组前

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`

我该如何解决这个问题?

【问题讨论】:

    标签: java android arrays string arraylist


    【解决方案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 颜色数组的长度为 3。

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

    4 第二次迭代 (i=1

     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 开始计数)。

    【讨论】:

    • myList 的大小必须 >= colors 的大小。将至少 3 个 HashMap 添加到 myList。这将排除异常。但我没有检查你的整个算法。
    【解决方案2】:
    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 String getGroupKey(String key, int i) {      
        ArrayList<String> uniqeList = getUniqKeyList(key);
        Collections.sort(uniqeList);
        return uniqeList.get(i);
    }
    

    由于您没有在这里检查相同的条件,因此您无法确定您的数据是一致的 - keyi 之间的依赖关系丢失了。

    【讨论】:

    • 好吧,看起来 uniqueList 的大小将是 0 或 1(如果 list 中没有重复值)。由于keyi之间没有关系,所以可以去掉getGroupKey方法中的i参数,把return语句改成return uniqeList.size()!=0?uniqueList.get(0):"";
    【解决方案3】:

    尝试检查列表中的索引是否可用,因为列表只有一个值,但您尝试获取多个值

    public String getGroupKey(String key, int i)
        {
            ArrayList<String> uniqeList = getUniqKeyList(key);
            Collections.sort(uniqeList);
            if (uniqeList.size() > i) // check here whether the list size is greater than index
            {
                return uniqeList.get(i);
            }
            else                      // else it returns an empty you can change by your concern
            {
                return "";
            }
        }
    

    而不是

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

    【讨论】:

      最近更新 更多