I am using the following code to keep list of objects in a MAP,
Map<String, List<Rows>> map = new HashMap<String, List<Rows>>()
while( rs.next() ) {
Rows row = new Rows();
/*
* code to initialize the values of row from the record
*/
String category = rs.getString("Cat");
if(!map.containsKey(category)){
map.put(category, new ArrayList<Rows>());
}
map.get(category).add(row);
}
is it possible to sort the values for each category?
how to remove an item in a specific category?
To remove item use
remove()method of map:map.remove(category)As far as I understand items of each category are stored in list, so sorting is simple:
Collections.sort(map.get(category))You can customize your sorting using your custom
Comparator.EDIT:
If however you wish to sort keys into your map you have to use
SortedMap, e.g.TreeMapand probably provide your comparator that knows to compare your categories. Otherwise keys will be sorted alphabetically.