I have created a HashMap using json response which I want to use its data group by month.
My json
[
{
"type":"CLAIMS PAID",
"category":"01. Less than 0",
"month":11,
"year":2020,
"count":173
},
{
"type":"CLAIMS PAID",
"category":"02. 1-5",
"month":11,
"year":2020,
"count":445
},
{
"type":"CLAIMS PAID",
"category":"03. 6-10",
"month":11,
"year":2020,
"count":362
},
{
"type":"CLAIMS PAID",
"category":"04. 11-15",
"month":11,
"year":2020,
"count":241
},
{
"type":"CLAIMS PAID",
"category":"05. 16-20",
"month":11,
"year":2020,
"count":130
},
{
"type":"CLAIMS PAID",
"category":"06. 21-25",
"month":11,
"year":2020,
"count":115
},
{
"type":"CLAIMS PAID",
"category":"07. 26-30",
"month":11,
"year":2020,
"count":66
},
{
"type":"CLAIMS PAID",
"category":"08. 31-35",
"month":11,
"year":2020,
"count":32
},
{
"type":"CLAIMS PAID",
"category":"09. More than 35",
"month":11,
"year":2020,
"count":251
},
{
"type":"CLAIMS PAID",
"category":"01. Less than 0",
"month":12,
"year":2020,
"count":257
},
{
"type":"CLAIMS PAID",
"category":"02. 1-5",
"month":12,
"year":2020,
"count":726
},
{
"type":"CLAIMS PAID",
"category":"03. 6-10",
"month":12,
"year":2020,
"count":722
},
{
"type":"CLAIMS PAID",
"category":"04. 11-15",
"month":12,
"year":2020,
"count":378
},
{
"type":"CLAIMS PAID",
"category":"05. 16-20",
"month":12,
"year":2020,
"count":207
},
{
"type":"CLAIMS PAID",
"category":"06. 21-25",
"month":12,
"year":2020,
"count":151
},
{
"type":"CLAIMS PAID",
"category":"07. 26-30",
"month":12,
"year":2020,
"count":94
},
{
"type":"CLAIMS PAID",
"category":"08. 31-35",
"month":12,
"year":2020,
"count":56
},
{
"type":"CLAIMS PAID",
"category":"09. More than 35",
"month":12,
"year":2020,
"count":351
},
{
"type":"CLAIMS PAID",
"category":"01. Less than 0",
"month":1,
"year":2021,
"count":254
},
{
"type":"CLAIMS PAID",
"category":"02. 1-5",
"month":1,
"year":2021,
"count":693
},
{
"type":"CLAIMS PAID",
"category":"03. 6-10",
"month":1,
"year":2021,
"count":538
},
{
"type":"CLAIMS PAID",
"category":"04. 11-15",
"month":1,
"year":2021,
"count":287
},
{
"type":"CLAIMS PAID",
"category":"05. 16-20",
"month":1,
"year":2021,
"count":194
},
{
"type":"CLAIMS PAID",
"category":"06. 21-25",
"month":1,
"year":2021,
"count":144
},
{
"type":"CLAIMS PAID",
"category":"07. 26-30",
"month":1,
"year":2021,
"count":110
},
{
"type":"CLAIMS PAID",
"category":"08. 31-35",
"month":1,
"year":2021,
"count":73
},
{
"type":"CLAIMS PAID",
"category":"09. More than 35",
"month":1,
"year":2021,
"count":372
},
{
"type":"CLAIMS PAID",
"category":"01. Less than 0",
"month":2,
"year":2021,
"count":225
},
{
"type":"CLAIMS PAID",
"category":"02. 1-5",
"month":2,
"year":2021,
"count":960
},
{
"type":"CLAIMS PAID",
"category":"03. 6-10",
"month":2,
"year":2021,
"count":733
},
{
"type":"CLAIMS PAID",
"category":"04. 11-15",
"month":2,
"year":2021,
"count":360
},
{
"type":"CLAIMS PAID",
"category":"05. 16-20",
"month":2,
"year":2021,
"count":200
},
{
"type":"CLAIMS PAID",
"category":"06. 21-25",
"month":2,
"year":2021,
"count":145
},
{
"type":"CLAIMS PAID",
"category":"07. 26-30",
"month":2,
"year":2021,
"count":107
},
{
"type":"CLAIMS PAID",
"category":"08. 31-35",
"month":2,
"year":2021,
"count":51
},
{
"type":"CLAIMS PAID",
"category":"09. More than 35",
"month":2,
"year":2021,
"count":271
}
]
Object class
public class Item{
private String type;
private String category;
private int month;
private int year;
private String monthName;
private String count;
}
JSONArray resArray = new JSONArray(jsonresponse);
for (int i = 0; i < resArray.length(); i++) {
JSONObject jo = resArray.getJSONObject(i);
Item ariData = new Item();
ariData.setType(jo.getString("type"));
ariData.setCategory(jo.getString("category"));
ariData.setYear(jo.getInt("year"));
ariData.setMonth(jo.getInt("month"));
ariData.setMonthName(getMonthFullName(jo.getInt("month")));
ariData.setCount(jo.getString("count"));
ariList.add(ariData);
}
I want to display same month's data in a RecyclerView so I have grouped by this json its Month for display in the RecyclerView. So I put it into a HashMap using..
Map<Integer, List<Item>> objectsPer = ariList.stream()
.collect(Collectors.groupingBy(Item::getMonth));
Problem is, If there is two years' data for example 2020-12 and 2021-01 then 2020-12 month's data display after 2021-01 data. I know this is happening because of the group by using month. I want to sort this HashMap using year also.
Edit
public class Item{
private String type;
private String category;
private String count;
private int total;
private YearMonth yearMonth;
}
JSONArray resArray = new JSONArray(response);
for (int i = 0; i < resArray.length(); i++) {
JSONObject jo = resArray.getJSONObject(i);
Item ariData = new Item();
ariData.setType(jo.getString("type"));
ariData.setCategory(jo.getString("category"));
ariData.setCount(jo.getString("count"));
YearMonth yMonth = YearMonth.of(jo.getInt("year"), jo.getInt("month"));
ariData.setYearMonth(yMonth);
ariList.add(ariData);
}
Map<YearMonth, List<Item>> objectsPer = ariList.stream()
.collect(Collectors.groupingBy(Item::getYearMonth));
Here I put date range in 2021-03 to 2021-06
see the debug I get for this date range after group by using year month
Here I put date range in 2020-11 to 2021-06


java.time.YearMonth.YearMonth, make anItemhave aYearMonthinstead of a year, a month and a month name. This class provides each of those and you can group by that attribute and the sorting will be as desired. You can instantiate it byYearMonth.of(int year, int month)and get the month name byyearMonth.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH);.NavigableMap<YearMonth, Set<Item>> objectsPer = ariList.stream().collect(Collectors.groupingBy(Item::getYearMonth, TreeMap::new, Collectors.toSet()));