Java API流文件到ArrayList [重复]

Posted

技术标签:

【中文标题】Java API流文件到ArrayList [重复]【英文标题】:Java API Stream File to ArrayList [duplicate] 【发布时间】:2020-09-08 00:45:31 【问题描述】:

直截了当。我相信这很容易解决,但不知何故我被卡住了。我有一个从主类调用的类 Uploader。此类流式传输带有学生列表的 txt 文件并将其打印出来。但是,我找不到解决方案,如何将这个txt文件存储在ArrayList中并在其他类中使用。文件根据 Student 类进行排序,该类覆盖 toString 方法以获取打印输出。我尝试使用 collect(collectors.toList()) 方法,但没有奏效。我想我错过了一些明显的东西。任何人都可以提出任何建议吗?谢谢。

这是我的学生类代码:

public class Student extends People 
private String collegeName;
private String studentType;
private final String studentID;

public Student(String firstName, String secondName, String telNo, String collegeName, String studentID, String studentType ) 
    super(firstName, secondName, telNo);
    this.studentID = studentID;
    this.collegeName = collegeName;
    this.studentType = studentType;



public String getStudentI() 
    String studentDet = getPeople() + collegeName + " " + studentType + " Student";
    return studentDet;



@Override
public String toString() 
    return "Student [name= " + firstName + ", second Name= " + secondName + " Telephone number= " + telNo + "college name= " + collegeName + " Student ID " + studentID + " Student Type= " + studentType + "]";

这里是上传器类:

public class Uploader 

private String fileName = null;


private void getPath() 

    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    setFileName(f.getAbsolutePath());
    System.out.println(getFileName());


void Uploader() 

    getPath();
    Path path = Paths.get(getFileName());

    try (Stream<String> lines = Files.lines(path)) 
        lines
                .flatMap(Uploader::lineToPerson)
                .forEach(System.out::println);

     catch (IOException ioe) 

        ioe.printStackTrace();


    



public static Stream<Student> lineToPerson(String line) 

    try 
        String[] elements = line.split(",");
        String firstName = elements[0];
        String secondName = elements[1];
        String telNo = elements[2];
        String collegeName = elements[3];
        String studentID = elements[4];
        String studentType = elements[5];


        Student p = new Student(firstName, secondName, telNo, collegeName, studentID, studentType);

        return Stream.of(p);


     catch (Exception e) 

    
    return Stream.empty();



public String getFileName() 
    return fileName;


public void setFileName(String fileName) 
    this.fileName = fileName;

【问题讨论】:

edit 您的问题并发布包含学生详细信息的 txt 文件中的示例数据。 【参考方案1】:

这里不需要使用 flatMap()。简单的地图转换可用于将线转换为学生对象并将它们收集到一个列表中。

        List<Person> personList = new ArrayList();
        try (Stream<String> lines = Files.lines(path)) 
            personList = lines
                    .map(this::lineToPerson)
                    .collect(Collectors.toList());   
         catch (IOException ioe)     
            ioe.printStackTrace();
        


        public Student lineToPerson(String line) 
            String[] elements = line.split(",");
            String firstName = elements[0];
            String secondName = elements[1];
            String telNo = elements[2];
            String collegeName = elements[3];
            String studentID = elements[4];
            String studentType = elements[5];

            return new Student(firstName, secondName, telNo, collegeName, studentID, studentType);

        

【讨论】:

谢谢,这正是我需要的。你为我节省了数小时的搜索时间。它向我展示了我还需要学习多少。再次感谢! 很高兴它解决了您的问题。学习愉快! 简单外行的话map和flatmap有什么区别? map - 对项目集合应用转换 (Collection) 并将它们收集到另一个集合中。 flatmap - 对项目集合 (Collection) 应用转换并将它们收集在单个集合 (Collection) 中。

以上是关于Java API流文件到ArrayList [重复]的主要内容,如果未能解决你的问题,请参考以下文章

使用流API将arraylist添加到arraylist

如何在TX中将txt行复制到ArrayList中?

排序ArrayList - JAVA [重复]

Java8 Stream 流API总结

java多线程java8的流操作api和fork/join框架

JAVA基础--JAVA API集合框架(其他集合类,集合原理)15