[英]Java Stream map or create new object of a FileStream
If I have a file which is tab separated, when I use the BufferedReader
object I can use Stream Api like filter, map, reduce, etc. methods so if Stream If I have a file which is tab separated, when I use the
BufferedReader
object I can use Stream Api like filter, map, reduce, etc. methods so if Stream
//first Line is Header Identified by #
# SUBSTANCE 2,60 25.04.2012 00:02:48 01.01.2000 24.04.2012
//this is my R Object which has attached two RN Objects
R 10 - 016-053-00-8 402-460-1 0 0 0 0 0 0
RN 10 0 DE (C16oderC18-n-Alkyl)(C16oderC18-n-alkyl)ammonium-2-((C16oderC18-n-alkyl)(C16oderC18-n-alkyl)carbamoyl)benzolsulfonat
RN 10 0 EN (C16orC18-n-alkyl)(C16orC18-n-alkyl)ammonium 2-((C16orC18-n-alkyl)(C16orC18-n-alkyl)carbamoyl)benzenesulfonate
I need to create a Object or a MAP (Header, rObject (rnObject,rnObject)) like a nested Array, so can I use the Stream API to do this? I need to create a Object or a MAP (Header, rObject (rnObject,rnObject)) like a nested Array, so can I use the Stream API to do this?
I already tried我已经试过了
BufferedReader in = new BufferedReader(new FileReader(absoluteFilePath));
List test = in.lines().limit(4).filter(identifier -> identifier.startsWith("RN")).map(line -> line.split("\\t")).collect(Collectors.toList());
So obviously it is not working since I get the output所以很明显它不起作用,因为我得到了 output
test.forEach(it -> System.out.println(it));
[Ljava.lang.String;@326de728
[Ljava.lang.String;@25618e91
So I thought I could create a Map with the Stream Api so I think the way I want it is not possible so I could just create a while lop and create so the objects what you think?所以我想我可以用 Stream Api 创建一个 Map,所以我认为我想要的方式是不可能的,所以我可以创建一段时间然后创建对象?
The best thing is to correctly identify the List as a List of String and do not use raw list.最好的办法是将列表正确识别为字符串列表,并且不要使用原始列表。
As the result of the split is a string array you have to wrap it as a list.由于拆分的结果是一个字符串数组,因此您必须将其包装为一个列表。
Then you will have a list of list, for this use flatMap to merge all the list into a unique one, then you will be able to use the collector and store it in a single list.然后您将拥有一个列表列表,为此使用 flatMap 将所有列表合并为一个唯一的列表,然后您将能够使用收集器并将其存储在一个列表中。
List<String> test = in.lines().limit(4).filter(identifier -> identifier.startsWith("RN")).map(line -> Arrays.asList(line.split("\\t"))).flatMap(List::stream).collect(Collectors.toList());
The main problem with your example is that you were storing a raw list of arrays objects.您的示例的主要问题是您存储了 arrays 对象的原始列表。 So when you call toString() into an array you get that hashReference starting with a square bracket.
因此,当您将 toString() 调用到数组中时,您会得到以方括号开头的 hashReference。
Hope this helps you!希望这对你有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.