java 用map实现聚合计算

问题:用java读取文件到内存实现简单的聚合计算,如下图,将如下文件先整理成{1={21,33},2={21,32}},这种形式然后就可以遍历map聚合了


    public static void main(String[] args) throws IOException { 

        String filename="/Users/data/test.json";
        String id="_id";
        String ts="_ts";
        count(filename,id,ts,tablename);
    }

//格式整理

    public static void count(String filename,String id,String ts) throws IOException {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(filename));
            // 第一种读取文件方式
            System.out.println("Reading the file using readLine() method: ");
            String contentLine;
            String str="";
            String id="";
            String time="";

            Map<String,List<String>> map_all=new HashMap<String,List<String>>();

            while ((contentLine = br.readLine()) != null) {

                if(!contentLine.equals("")) {
                    Map maps = (Map) JSON.parse(contentLine);

                    device_id = maps.get(id).toString();
                    gps_time = maps.get(ts).toString();

                    List<String> times = new ArrayList<String>();
                    if (!map_all.containsKey(device_id)) {
                        times.add(gps_time);
                        map_all.put(device_id, times);
                    } else {
                        map_all.get(device_id).add(gps_time);
                    }
                }
            }
             System.out.println(map_all);
             
             countTime(map_all);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(br!=null){
                br.close();
            }

        }
    }

//聚合

  public static Map<String,String> countTime(Map<String,List<String>> map_all) throws ParseException {
        Map<String,String> map_result=new HashMap<String, String>();

        for (Map.Entry<String,List<String>> entry : map_all.entrySet()) {
            entry.getKey();
            List<String> list1=entry.getValue();
            long dif_all=0;
            for(int i=0;i<list1.size()-1;i++){

                dif_all=dif_all+list1.get(i);
            }
            map_result.put(entry.getKey(),dif_all);
        }
        return map_result;
    }

个人观点,欢迎指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值