Android - 按日期(而不是按日期时间)对对象列表进行分组

时间:2021-03-16 12:38:57

标签: java android hashmap

messages:[{
       "date": "2021-03-04T14:20:38+00:00",
       "Country": "Bangladesh",
       "Profession": "X",
       "Salary": "100",
       },
       {
       "date": "2021-03-04T14:21:38+00:00",
       "Country": "Bangladesh",
       "Profession": "Y",
       "Salary": "101",
       },
       {
       "date": "2021-03-05T14:20:38+00:00",
       "Country": "Bangladesh",
       "Profession": "Z",
       "Salary": "102",
       },
{
       "date": "2021-03-06T14:20:38+00:00",
       "Country": "Bangladesh",
       "Profession": "Z",
       "Salary": "102",
       }]

这是我的消息的示例数组,我需要按日期对上面的数组进行分组。

到目前为止我已经尝试过,

 Map<String, List<Message>> map = new HashMap<String, List<Message>>();
        for (Message mes : messages) {
            String key = mes.getTimestamp();
            if (map.get(key) == null) {
                map.put(key, new ArrayList<Message>());
            }
            map.get(key).add(person);
        }

但这是使用日期时间对数组进行分组的代码。我只需要按日期对上面的数组进行分组。 有什么办法可以做到这一点吗?

我需要一个像下面这样的数组:

message:[{
2021-03-04:[{"date": "2021-03-04T14:20:38+00:00",
       "Country": "Bangladesh",
       "Profession": "X",
       "Salary": "100"},
{
       "date": "2021-03-04T14:21:38+00:00",
       "Country": "Bangladesh",
       "Profession": "Y",
       "Salary": "101",
       }],
2021-03-05:[{
"date": "2021-03-05T14:20:38+00:00",
       "Country": "Bangladesh",
       "Profession": "X",
       "Salary": "102"
}],
2021-03-06:[{
"date": "2021-03-06T14:20:38+00:00",
       "Country": "Bangladesh",
       "Profession": "X",
       "Salary": "102"
}]
}]

0 个答案:

没有答案