分组统计个数

【问题】

I have a text file with content like, with 792 lines:

der 17788648
und 14355959
die 10939606
Die 10480597

Now I want to compare if "Die" and "die" are equal in lowercase. So if two Strings in lowerCase are equal, copy the word into a new text file in lowerCase and sum the values.

Expected output:

der 17788648
und 14355959
die 21420203

I have that so far:

  try {
        BufferedReader bk = null;
        BufferedWriter bw = null;

        bk = new BufferedReader(new FileReader("outagain.txt"));
        bw = new BufferedWriter(new FileWriter("outagain5.txt"));

        List<String> list = new ArrayList<>();
        String s = "";
        while (s != null) {
            s = bk.readLine();
            list.add(s);
        }


        for (int k = 0; k < 793; k++) {
            String u = bk.readLine();
            if (list.contains(u.toLowerCase())) {

                //sum values?

            } else {
                bw.write(u + "\n");
            }
        }

        System.out.println(list.size());

    } catch (Exception e) {
        System.out.println("Exception caught : " + e);
    }

 

【回答】

       这是简单的分组汇总,JAVA没有通行的结构化计算类库,硬编码很繁琐。可以用SPL实现,只要一句。

A
1=file("d:\\data.txt").import(;," ").groups(lower(#1);sum(#2))
2=file("d:\\result.txt").export(A1; " ")

A1:按第1个字段的小写分组,按第2个字段汇总。

A2:如果想把结果写到文件,可以再加此句。

这段代码可以方便地集成进Java,参考Java 如何调用 SPL 脚本

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值