1

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.

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. Commented Mar 31, 2022 at 10:37
  • How to read and parse CSV file in Java shows an option using the Streams API. But it needs some supporting methods. Commented Mar 31, 2022 at 10:41
  • 1
    Some similar questions, many with answers. Commented Mar 31, 2022 at 10:42
  • Maybe this will help stackoverflow.com/questions/49660669/… Commented Mar 31, 2022 at 10:43

1 Answer 1

-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).

Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.