// 处理网络请求的data数据
public List<PostsSection> disposeData(List<PostsInfo> list) {
// 分类
LinkedHashMap mPostsMap = new LinkedHashMap<>();
for (int i = 0; i < list.size(); i++) {
String yearTime = TimeFormatUtils.getYearTime(list.get(i).getCreateTime());
if (mPostsMap.containsKey(yearTime)) {
// 如果包含就把此info加到list1<PostsInfo>集合中
ArrayList<PostsInfo> list1 = mPostsMap.get(yearTime);
list1.add(list.get(i));
mPostsMap.put(yearTime, list1);
} else {
// 如果不包含就创建一个新的list
ArrayList<PostsInfo> list2 = new ArrayList<>();
list2.add(list.get(i));
mPostsMap.put(yearTime, list2);
}
}
// 通过以上两步得到一个分类好的HashMap,遍历此hashMap
Iterator<String> it = mPostsMap.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
mPostsSectionSections.add(new PostsSection(true, key));
List<PostsInfo> postsInfos = mPostsMap.get(key);
// 遍历list
for (PostsInfo postsInfo : postsInfos) {
mPostsSectionSections.add(new PostsSection(postsInfo));
}
}
return mPostsSectionSections;
}
通常作为一个前端开发人员都不太想写分类这样的逻辑,都比较希望让后台处理掉,自己拿到一个分好类的数据,在开发过程中当后台赶需求来不及分类或者不愿意分类时候,咱们就可以通过上述方式可以自己分类。如果是排序就用Collection.sort();
Collections.sort();