0

I have an ArrayList<HashMap<String, String>> which stores certain values associated with their keys.

map = new HashMap<String,String>();
map.put(NEWTITLE, title);
map.put(TITLE, description);
map.put(THUMBNAILPATH, thumbnail);
map.put(BODY, body);
map.put(NEWID, newId);
map.put(ID, newsId);
map.put(PUBLISHER,publisherName);

myNList.add(map);

I want to group the above list according to the key "NEWID" any idea how this can be done?

4
  • What do you mean with 'group' ? Do you want one ArrayList for each different NEWID (containing of course only the entries with the corresponding NEWID)? Or do you want to store the entries of the ArrayList according the NEWID? Commented Apr 22, 2014 at 8:49
  • use hashmap inside hashmap key: HashMap<String, HashMap<String, String>> Commented Apr 22, 2014 at 8:53
  • Nicola I am fetching some data from a URL. Each entry contains items which have a NEWID in them. NEWID can be same for multiple entries. I want another ArrayList which would group the data accroding to this NEWID Commented Apr 22, 2014 at 8:54
  • @user2758757 Can you provide an example please? Commented Apr 22, 2014 at 8:55

1 Answer 1

1

By "group" I understand that you mean "sort" (newId 2 will be next to newId 3). Provide a Comparator<HashMap<String, String>> implementation that gives the ordering that you want, and use Collections.sort(List, Comparator)

If you want to "group" in the usual sense, you cannot use that structure, you need another Map (using newId as the key).

Map<String, List<Map<String, String>>>
Sign up to request clarification or add additional context in comments.

1 Comment

I have this ArrayList<HashMap<String, String>>. Which contains the key NEWID. Multiple entries can have the same NEWID. I want to group the entries with similar NEWID. Then associate a array of color to the entries with similar NEWID

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.