Group & Aggregate by a Specified Field
【Question】
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]);
}
}}
I need 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|||||
The age is in the 28th location in each row of a file. Here I want to read the file and collect all Age values in array and display how many people are there with the age 26 and how many are there with the ages 24 and 19.
【Answer】
Grouping and aggregating records by a specified field is one of the basic structured computations. But it’s difficult to code it in Java because the language lacks corresponding class library. Here I express the computation in SPL (Structured Process Language). The code is simple and easy to understand:
A |
|
1 |
=file("d:\\source.txt").import(;,"|") |
2 |
=A1.groups(#28;count(~)) |
A1: Read in contents of source.txt.
A2: Group records by the 28th field and count records in each group.
The SPL script is easily integrated in a Java program, see How to Call an SPL Script in Java for more explanations.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL