You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Firestore数据去重:合并同日期项并生成集合

解决Firestore数据按日期合并去重的方案

Hey there! 看起来你已经顺利完成了Firestore数据的获取和视图渲染,现在要解决的是按日期合并重复数据的问题对吧?我给你分享一个简单可行的实现思路和代码示例~

核心思路

我们可以借助Map集合,以日期字符串作为键,存储合并后的每日数据对象。遍历从Firestore拿到的每条数据时:

  • 如果当前日期已经在Map中存在,就把对应数值字段(比如存款、取款金额)累加;
  • 如果日期不存在,就直接将这条数据作为新条目存入Map。
    最后把Map中的值转换成List,就得到了去重合并后的数据集。

代码修改示例

你可以把原来onSuccess方法里的for循环替换成以下逻辑:

@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
    mProg.setVisibility(View.GONE);
    if (!queryDocumentSnapshots.isEmpty()) {
        List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
        Toast.makeText(getActivity(), "payments collected!\n size is " + list.size(), Toast.LENGTH_SHORT).show();
        
        // 1. 创建Map用于存储按日期合并后的数据
        Map<String, Daily> mergedDailyMap = new HashMap<>();
        
        for (DocumentSnapshot snapshot : list) {
            CustomerPayment acc = snapshot.toObject(CustomerPayment.class);
            // 把CustomerPayment转成Daily对象(如果两个类属性一致,也可以直接用CustomerPayment处理)
            Daily daily = new Daily(
                acc.getmDal(), 
                acc.getmDeposit(), 
                acc.getmWithdrawed(), 
                acc.getmReached(), 
                acc.getmAuth(), 
                acc.getmDate()
            );
            
            String dateKey = daily.getmDate();
            if (mergedDailyMap.containsKey(dateKey)) {
                // 2. 日期已存在,累加数值字段
                Daily existingDaily = mergedDailyMap.get(dateKey);
                existingDaily.setmDal(existingDaily.getmDal() + daily.getmDal());
                existingDaily.setmDeposit(existingDaily.getmDeposit() + daily.getmDeposit());
                existingDaily.setmWithdrawed(existingDaily.getmWithdrawed() + daily.getmWithdrawed());
                existingDaily.setmReached(existingDaily.getmReached() + daily.getmReached());
                // 注意:如果同一天的mAuth字段有不同值,这里需要根据需求处理,比如保留第一个/拼接所有
            } else {
                // 3. 日期不存在,直接存入Map
                mergedDailyMap.put(dateKey, daily);
            }
        }
        
        // 4. 将Map中的合并后数据转成List,更新适配器
        accounts.clear();
        accounts.addAll(mergedDailyMap.values());
        adapter.notifyDataSetChanged();
    } else {
        Toast.makeText(getActivity(), "No item", Toast.LENGTH_SHORT).show();
    }
}

几个需要注意的细节

  • 模型类的Getter/Setter:你的Daily类需要补充对应的get和set方法(比如getmDal()、setmDal(int)),否则无法修改累加后的数值。
  • 特殊字段处理:如果同一天的多条数据中mAuth字段值不同,你需要明确业务规则——是保留第一个值、拼接所有值,还是忽略该字段?上面的代码默认保留第一条数据的mAuth,你可以按需调整。
  • 数据溢出问题:如果数值字段(比如存款金额)可能很大,建议把int类型改成long,避免整数溢出。
  • 排序需求:如果需要合并后的列表按日期排序,可以在转成List后添加排序逻辑:
    List<Daily> mergedList = new ArrayList<>(mergedDailyMap.values());
    Collections.sort(mergedList, (d1, d2) -> {
        // 假设日期格式是yyyy-MM-dd,可直接字符串比较;否则需要转成Date对象排序
        return d1.getmDate().compareTo(d2.getmDate());
    });
    accounts.clear();
    accounts.addAll(mergedList);
    

内容的提问来源于stack exchange,提问作者user8950968

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:30:57