当前位置: 首页 > 知识库问答 >
问题:

使用Android中的列表值对HashMap进行排序(map)

松俊才
2023-03-14

我已经使用json响应创建了一个HashMap,我希望按月使用它的数据组。

我的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
   }
]

对象类

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

                }
Map<Integer, List<Item>> objectsPer = ariList.stream()
                                .collect(Collectors.groupingBy(Item::getMonth));
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));

共有1个答案

徐高韵
2023-03-14

正如deHaar所评论的,您应该使用java.time.YearMonts类,而不是单独的int字段来表示year和Month。并且不需要存储月份名称,因为可以通过调用YearMonth#GetMonthmonth#GetDisplayName来动态确定月份名称。

重新定义您的类,使用更少的字段:字符串类型、字符串类别、YearMonth YearMonth和int count。

(count不应该是整数而不是string吗?)

public record Item ( String type, String category, YearMonth yearMonth,  int count ) {}

在映射中收集对象。使用可导航地图和可导航集保持按YearMonth排序。

也许可以使用TreemapTreeset作为实现的选择。在构造Treeset对象时,传递comparator来组织排序。

NavigableMap< YearMonth , NavigableSet< Item > > = …
 类似资料:
  • 边走边学Java(Python背景)。简单的单词计数程序在Java7代码(不能用J8!)。 我有一个单词的哈希图:计数对。现在我需要按计数(递减顺序)排序,并打破按字母顺序使用word的联系。 我正在寻找对这个想法的反馈: 遍历HashMap中的映射项(me) 使用me.getkey=K和me.getvalue=v new map.entry reverse_me=(V,K){不确定此语法} 将r

  • 编辑:对于同样的问题,我尝试编写一个比较器。但它不起作用

  • 问题内容: 我在Python中有两个列表 我想对第一个列表进行排序,并使用结果对第二个列表进行排序。 换句话说,结果应为: 我知道如何分别对每个列表进行排序,但是如何使用对另一个列表进行排序所产生的索引排列来对一个列表进行排列呢? 问题答案: 施瓦兹变换

  • rank ▲ ✰ vote url 49 432 198 616 url 通过列表中字典的值对列表进行排序 我的到了一个字典的列表,我想对字典的值进行排序. [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] 对name进行排序,应当是: [{'name':'Bart', 'age':10}, {'name':'Homer', 'age

  • 问题内容: 我有一组三个列表项,它们希望在页面加载时从高到低自动显示。理想情况下使用jquery或javascript。 每个列表项都需要有自己的ID,因为它们每个都有各自的背景图像。数字必须是文本节点,以便用户可以编辑它们。 问题答案: 这可能是最快的方法,因为它不使用jQuery: 像下面这样调用函数: 您可以以相同的方式对其他列表进行排序,如果列表类在同一页面上还有其他元素,则应给您的ul一

  • 我有: 三个表:、和. CursorLoader和CursorAdapter:和. ListView:一个简单的ListView. 是否可以坚持使用并使用另一个表中的列实现对的排序?