简体   繁体   中英

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

I have a ArrayList >> which holds certain key-value entries. Like:-

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" can be similar for multiple entries.

Also I have an Array of colours:-

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

I want a mew group with all the entries with same "NewId" together and assign them the first colour, other entries with next similar "NewId" with the second colour and so on till items with first 10 same "NewId" get assigned with their respective colours.

eg:- before grouping

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

After grouping,

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

What I have done yet:-

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);

But I get an error:-

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

How can I solve this?

If your question about exception, then the problem in your particular case in:

1 This method always returs only 1-element array list:

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 array has length 3.

3 First iteration while i=0 will be ok.

4 Second iteration (i=1 < color.length=3) has problem in:

 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), but uniqeList has length 1 (numeration from 0).

Try to check whether the index is available in the list or not because list having only one value but you are trying to get multiple

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 "";
        }
    }

instead of

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;
}

you have a condition if to add an item to the list or not

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

since you don't check the same condition here you can't be sure your data is consistent - the dependency between key and i is lost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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