2

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?

3 Answers 3

1

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

1
  • myList's size must be >= colors' size. Add to myList at least 3 HashMaps. This will exclude exception. But I didn't check your whole algorithm. Commented Apr 24, 2014 at 6:53
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);
}
0
0
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.

1
  • well, looks like the size of the uniqueList will be 0 or 1 (if there are no duplicate values in the list). Since there is no relation between key and i, you can remove the i parameter from getGroupKey method and change the return statement to return uniqeList.size()!=0?uniqueList.get(0):""; Commented Apr 24, 2014 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.