3

I'm trying to understand java 8's Streams. Currently, I have a .txt file with this format:

2011年11月28日 02:27:59 2011年11月28日 10:18:11 Sleeping 
2011年11月28日 10:21:24 2011年11月28日 10:23:36 Toileting 
2011年11月28日 10:25:44 2011年11月28日 10:33:00 Showering 
2011年11月28日 10:34:23 2011年11月28日 10:43:00 Breakfast 

Those 3 "items" are always separated by a TAB. What i want to do is to declare a class, MonitoredData, with attributes(of type String)

start_time end_time activity

What i want to achieve is to read the data from the file using streams and create a list of objects of type MonitoredData.

After reading about Java 8 i managed to write the following but then I reached a dead end

public class MonitoredData {
 private String start_time;
 private String end_time;
 private String activity;
 public MonitoredData(){}
 public void readFile(){
 String file = "Activities.txt"; //get file name
 //i will try to convert the string in this DateFormat later on
 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
 //Store the lines from the file in Object of type Stream
 try (Stream<String> stream = Files.lines(Paths.get(file))) { 
 stream.map(line->line.split("\t"));
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
}

Well, somehow I have to split the each line and store it in a Object appropriate to MonitoredData's attribute. How do i do that?

mark42inbound
3741 gold badge5 silver badges19 bronze badges
asked May 18, 2018 at 19:26
3
  • 2
    stream.map(line -> line.split("\t")).map(v -> new MonitoredData(sd.parse(v[o]), sd.parse(v[1]), v[2]).collect(toList()); Commented May 18, 2018 at 19:30
  • 2
    @Boris the Spider ...sd.parse(v[o])... should be ...sd.parse(v[0])... ;) Commented May 18, 2018 at 19:37
  • SimpleDateFormat sd. You wrote the code... Commented May 18, 2018 at 20:27

1 Answer 1

6

All you have to do is add another map operation as shown below:

 stream.map(line -> line.split("\t"))
 .map(a -> new MonitoredData(a[0], a[1], a[2]))
 .collect(Collectors.toList());

then collect to a list with the toList collector.

answered May 18, 2018 at 19:37

4 Comments

@BoristheSpider not sure if that's what the OP wants as in the MonitoredData class start_time and end_time are strings whereas SimpleDateFormat.parse will yield a Date instance.
Hmm - fair point - does beg the question of why the SDF is there to begin with... (have a +1)
@BoristheSpider Thanks!. the OP mentioned "i will try to convert the string in this DateFormat later on" , so I am assuming the SimpleDateFormat instance is for later operations that they want to perform.
Using "\t" as regex is not wrong. "\\t" is a special-character pattern referring to <tab> whereas "\t" just matches the <tab> character literally. The result is the same.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.