user3534519
user3534519

Reputation: 99

Group ArrayList<HashMap<String, String>> by a certain key

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?

Upvotes: 0

Views: 455

Answers (1)

SJuan76
SJuan76

Reputation: 24895

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

Upvotes: 1

Related Questions