Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

Java 8 Streams

Hey, Right now I'm stuck on a pretty specific problem concerning the java.nio API.

I have a class Data with the constructor

public Data(String name, String firstName, String stdtId, String skz, List<Integer> submitted, List<Double> points, double test)

and a .csv file that contains values like:

1;NAME;FIRSTNAME;1234567;521; 1;1;0;1;1;1;1;1;1;1;0;  19;23;22;21;;16;17;18;19;20;21;9

per line. and the values: [1;1;0;1;1;1;1;1;1;1;0;] and: [19;23;22;21;;16;17;18;19;20;21;9] should be put in Lists (because of the constructor for the Data object)

I want to use a Stream to read the file, but I do not know how to make Lists inside a Stream?

My class looks like this so far:

         final String path = "data.csv";
         final int NAME = 1;
         final int FIRSTNAME = 2;
         final int STDID = 3;
         final int SKZ = 4;
         final int SUBMITTED = 5;  //most probably wrong the list should be 11 items long
         final int POINTS = 6; //most probably wrong the list should be 11 items long
         final int TEST = 7;



        List<Data> list =
                Files.lines(Paths.get(path))
                        .skip(1)        //skip line 1
                        .map(line -> line.split(";"))                   //split each line at ; and make it to an array
                        .map(elem -> new Data(  elem[NAME],
                                                elem[FIRSTNAME],
                                                elem[STDID],
                                                elem[SKZ],
                                                //TODO: add SUBMITTED List,
                                                //TODO: add POINTS List,
                                                Double.parseDouble(elem[TEST])) //make each line a data object
                        .collect(Collectors.toList()));

        for(Data d:list){
            System.out.println(d);
        }
Lukas Baumgartner
Lukas Baumgartner
14,817 Points

If anyone is interested, I solved it:

        List<Data> dataList =
                Files.lines(Paths.get(path))
                        .skip(1)        //skip line 1
                        .map(line -> line.split(";",-1))                   //split each line at ; and make it to an array
                        .map(elem -> new Data(  elem[NAME],
                                                elem[FIRSTNAME],
                                                elem[STDID],
                                                elem[SKZ],
                                                Arrays.stream(Arrays.copyOfRange(elem, 5, 15))
                                                        .map(String::trim)
                                                        .mapToInt(Integer::parseInt)
                                                        .boxed()
                                                        .collect(Collectors.toList()),
                                                Arrays.stream(Arrays.copyOfRange(elem, 16, 26))
                                                        .map(itm -> (itm.isEmpty() ? "0" : itm))
                                                        .map(String::trim)
                                                        .mapToDouble(Double::parseDouble)
                                                        .boxed()
                                                        .collect(Collectors.toList()),
                                                elem[TEST].isEmpty() ? 0 : Double.parseDouble(elem[TEST]))) //make each line a data object
                        .sorted(Comparator.comparing(Data::toString))
                        .collect(Collectors.toList());