繁体   English   中英

从ArrayList获取密钥 <HashMap<String, String> &gt;&gt;放入另一个数组

[英]Get Keys from ArrayList<HashMap<String, String>>> into another array

我有一个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颜色数组的长度为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开始计算)。

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

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);
}
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之间的依赖关系。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM