0

I need to read data from a CSV file and get an List< MyObject > as a result.

For example I have a Entry class:

public class Entry {

    private String productId;
    private LocalDate date;
    private String state;
    private String category;
    private Double amount;

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }
}

And a CSV file:

Order Date  State   Product ID  Category    Product Name    Sales
08.11.2016  Kentucky    FUR-BO-10001798 Furniture   Bush Somerset Collection Bookcase   261,96
08.11.2016  Kentucky    FUR-CH-10000454 Furniture   Hon Deluxe Fabric Upholstered Stacking Chairs, Rounded Back 731,94

Can I get an List< Entry > from the CSV file using Stream API? Later I should be able to get data from the list and for example get total sales, total sales for a specific date and so on.

Thanks.

lopaxxxkzz
  • 51
  • 4
  • What's the delimiter in your CSV? `\t`? If you're doing Real Work™, OpenCSV would be your choice instead of rolling your own though. – Kayaman Mar 31 '22 at 10:37
  • [How to read and parse CSV file in Java](https://mkyong.com/java/how-to-read-and-parse-csv-file-in-java/) shows an option using the Streams API. But it needs some supporting methods. – Scratte Mar 31 '22 at 10:41
  • 1
    Some [similar questions](https://www.google.com/search?q=site:stackoverflow.com+java+parse+csv+streaming), many with answers. – Hovercraft Full Of Eels Mar 31 '22 at 10:42
  • Maybe this will help https://stackoverflow.com/questions/49660669/parsing-csv-file-using-java-8-stream – Nero Mar 31 '22 at 10:43

1 Answers1

-1

Yes it is possible. You need to read the file in a BufferedReader and apply the method lines

Returns a Stream, the elements of which are lines read from this BufferedReader

The code should be something similar to that:

File file = new File(path);
InputStream is = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));

// br.lines() is a stream
List<Entry> entries = br.lines()
  .map(line -> convert(line))
  .collect(Collectors.toList());

...


// Helper method to convert a single line (as string) in a Entry object
public Entry convert(String line) {  
    Entry entry = new Entry();
    // Apply your logic to parse line and populate entry properties
    ...
    return entry;
}

Note that your example is not a csv (comma separated value) file, but this code works for any text file where each record is presented in a single line (as in your example).

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • As always would be interesting knowing why it is not correct and downvoted. The question *Can I get an List< Entry > from the CSV file using Stream API* is clearly answered here – Davide Lorenzo MARINO Mar 31 '22 at 11:11