Create a list of objects from a file using lambda expressions in Java

The lambda expression

public List<Conference> readInputStream(InputStream inputStream) throws IOException { 
    // target list 
    List conferenceModelCollection; 
    try ( 
          // open the url stream, wrap it an a few "readers" 
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { 
        // read the file, line by line 
        conferenceModelCollection = reader.lines() 
        // skip the first 4 lines - contain metadata 
            // take the line and convert into an object 
            // sort the lines by date 
            .sorted((f1, f2) -> f1.getBegin().compareTo(f2.getEnd())) 
            // add to the existing collection 
    return conferenceModelCollection; 

Example of the converter

private Conference convertToConference(String dataLine) { 
    String[] data = dataLine.split("\\|"); 
    int dataPosition = 1; 
    Conference conference = new Conference(); 
    conference.setBegin(LocalDate.parse(data[dataPosition++], DATE_FORMATTER)); 
    return conference; 

Read the file

private List<Conference> readCalendarDataFromUrl() throws IOException { 
    URL url = new URL(urlString); 
    return readInputStream(url.openStream()); 

You could be interested in

Java Streams and Lambda quick introduction

Quick notes about Java Streams and Lambda expressions

Super fast WebApp built by Marco using SpringBoot 3 and Java 17 hosted by Infomaniak in Switzerland