关于后台返回数据为一个整体,我们需要拿某一个字段出来单独以这个字段进行分类的需求

来源:互联网 发布:开票软件重装 编辑:程序博客网 时间:2024/04/29 01:16
// 处理网络请求的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();
阅读全文
1 0
原创粉丝点击