拆分流并从文本文件中放入列表(Split stream and put into list from text file)

编程入门 行业动态 更新时间:2024-10-10 17:29:19
拆分流并从文本文件中放入列表(Split stream and put into list from text file)

如何使用将我从文本文件中读取的所有元素放入ArrayList < MonitoredData >中,其中monitoredData类具有以下3个私有变量: private Date startingTime, Date finishTime, String activityLabel ;

File Activities.txt文本如下所示:

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

等等....

前两个字符串由一个空格分隔,然后是2个标签,再一个空格,2个标签。

String fileName = "D:/Tema 5/Activities.txt"; try (Stream<String> stream = Files.lines(Paths.get(fileName))) { list = (ArrayList<String>) stream .map(w -> w.split("\t\t")).flatMap(Arrays::stream) // \\s+ .collect(Collectors.toList()); //list.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); }

How can I put all the elements I read from a text file into an ArrayList < MonitoredData > using streams, where monitoredData class has these 3 private variables: private Date startingTime, Date finishTime, String activityLabel;

The text File Activities.txt looks like this:

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

and so on....

The first 2 strings are separated by one blank space, then 2 tabs, one space again, 2 tabs.

String fileName = "D:/Tema 5/Activities.txt"; try (Stream<String> stream = Files.lines(Paths.get(fileName))) { list = (ArrayList<String>) stream .map(w -> w.split("\t\t")).flatMap(Arrays::stream) .collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); }

最满意答案

你需要引入一个工厂来创建MonitoredData ,例如我使用一个Function从String[]创建一个MonitoredData :

Function<String[],MonitoredData> factory = data->{ DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try{ return new MonitoredData(format.parse(data[0]),format.parse(data[1]),data[2]); // ^--startingTime ^--finishingTime ^--label }catch(ParseException ex){ throw new IllegalArgumentException(ex); } };

那么您的代码在流上运行应该如下所示,并且您不需要使用收集器#toCollection来转换结果:

list = stream.map(line -> line.split("\t\t")).map(factory::apply) .collect(Collectors.toCollection(ArrayList::new));

you need introduce a factory to create the MonitoredData, in example I'm using a Function to create a MonitoredData from String[]:

Function<String[],MonitoredData> factory = data->{ DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try{ return new MonitoredData(format.parse(data[0]),format.parse(data[1]),data[2]); // ^--startingTime ^--finishingTime ^--label }catch(ParseException ex){ throw new IllegalArgumentException(ex); } };

THEN your code operate on a stream should be like below, and you don't need casting the result by using Collectors#toCollection:

list = stream.map(line -> line.split("\t\t")).map(factory::apply) .collect(Collectors.toCollection(ArrayList::new));

更多推荐

本文发布于:2023-07-17 14:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并从   文本文件   放入   列表   Split

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!