简单分组汇总

【问题】

public class Data {

         public static void main(String args[]) throws IOException {
         String gender;
         BufferedReader in = new BufferedReader(new FileReader("File Path"));
         String str;
         List<String> list = new ArrayList<String>();
         while ((str = in.readLine()) != null) {
         list.add(str);
         }

         String[] stringArr = list.toArray(new String[0]);
         System.out.println(list.size());
         Map<String, List<String>> genderNameList = new HashMap<String, List<String>>();

        for (int i = 1; i < list.size(); i++) {
        String line = list.get(i);
        String[] values = line.split("\\|");
        genderNameList.get(values[1]).add(values[1]);

    }
}}

Facing problem with the code. I have to read a file Vicky.txt which contains the data like

1. 123456|Mr|Peter|..........|19|||||
2. 556667|Ms|amith|..........|26|||||
3. 098765|Mr|kevinpeter|..........|24|||||
4. 675584|Mr|kapaul|..........|26|||||
5. 234906|Ms|zim|..........|24|||||
6. 123456|Ms|tom|..........|24|||||

Where the age is in the 28Th location in each row of a file. Here I have to read the file and collect all Age values in array and I have to display how many people are there with the age as 26 and how many are there with the age 24 and 19.

Here is the code that I did but its not working....

【回答】

按照指定字段分组汇总,这是简单的结构化计算,但JAVA缺乏相应类库,代码会很难写。可用SPL实现,代码简单易懂:

A
1=file("d:\\source.txt").import(;,"|")
2=A1.groups(#28;count(~))

A1:读取文本文件source.txt中的内容。

A2:按照第28个字段分组汇总。

上述代码很容易集成到JAVA,参考Java 如何调用 SPL 脚本

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值