简体   繁体   English

如何在Java中为.txt文件的每个唯一列值创建ArrayList?

[英]How to create an ArrayList for every unique column value of a .txt file in Java?

I'm writing a program that reads a .txt file line by line, and I want to create for every 'unique' value of one column, an ArrayList . 我正在编写一个程序,逐行读取.txt文件,并且我想为每一列的“唯一”值创建一个ArrayList

What the .txt file looks like: .txt文件如下所示:

NAME          AGE          COUNTRY          PHONE NUMBER
Peter         28           USA              00112233
John          25           England          11223344
Justin        22           Australia        22334455
Daisey        24           Canada           33445566
Harry         27           England          44556677
Laura         25           England          55667788
Gary          28           USA              66778899

For example, I want to create for each nationality an ArrayList, with the corresponding names as elements. 例如,我想为每个国籍创建一个ArrayList,以相应的名称作为元素。

So, this: 所以这:

ArrayList USA:       Peter, Gary
ArrayList England:   John, Harry, Laura
ArrayList Australia: Justin
ArrayList Canada:    Daisey

I don't want to create the ArrayLists myself, before running the program. 在运行程序之前,我不想自己创建ArrayLists Because the real column I'm talking about has lots of different values, so lots of ArrayList should be made. 因为我要说的实际列具有很多不同的值,所以应该制作很多ArrayList Does anyone know how I can let the program create an Arraylist for every unique nationality, and add the corresponding names to this ArrayList ? 有谁知道我如何让程序为每个唯一的国籍创建一个Arraylist ,并将相应的名称添加到该ArrayList

Well,it's easier to work with domain objects like: 好吧,使用像这样的域对象会更容易:

class User {
    String name;
    String country;
    int age;
    String phone;
    // getters and setters omitted
}

Somewhere in your code there will be method like: 在代码中的某处将有类似以下方法:

List<User> readfromFile(String fileName) {
    // create User for each read line
}

And then use Stream Api to group by needed column like: 然后使用Stream Api按所需列进行分组,例如:

Map<String, List<User>> usersByCountry = users.stream().collect(Collectors.groupingBy(User::getCountry));

If you want just user names grouped by country: 如果只想按国家/地区分组用户名:

Map<String, List<String>> userNamesByCountry = users.stream().collect(Collectors.groupingBy(User::getCountry, LinkedHashMap::new, Collectors.mapping(User::getName, Collectors.toList())));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何读入 txt 文件的 1 个特定列并将其存储到 Array 或 ArrayList [Java] - How to read in 1 specific column of a txt file and store into an Array or ArrayList [Java] 如何在java中找到2D ArrayList列的唯一值? - How to find unique value of a column of a 2D ArrayList in java? Java - 如何将ArrayList对象写入txt文件? - Java - How to write ArrayList Objects into txt file? Java:无法存储a.txt文件的每一段。 在 ArrayList <arraylist<string> &gt; </arraylist<string> - Java: unable to store every paragraph of a .txt file. in a ArrayList<ArrayList<String>> 如何在Java中创建txt文件? - How to create a txt file in Java? 在java中显示带有arraylist的.txt文件 - display a .txt file with arraylist in java 每次用户在Java中输入值时,如何创建更新ArrayList的表? - How can I create a table that updates an ArrayList every time a user enters a value in Java? 如何基于.txt文件信息创建对象并分配给arraylist? - How to create object based off .txt file information and assign to an arraylist? 创建一个应用程序-Java-从.txt文件创建一个arraylist,并为arraylist的特定行 - Creating an app - Java - create an arraylist from a .txt file and toast specific line of arraylist 如何从java MVC中的arraylist .TXT文件显示JTable中的记录? - How to display records in a JTable from an arraylist .TXT file in java MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM