簡體   English   中英

Java Stream map 或創建新的 ZA8CFDE6331BD59EB2AC9Z6F8911C4B66 的文件

[英]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

 //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?

我已經試過了

    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());

所以很明顯它不起作用,因為我得到了 output

    test.forEach(it -> System.out.println(it));
    [Ljava.lang.String;@326de728
    [Ljava.lang.String;@25618e91

所以我想我可以用 Stream Api 創建一個 Map,所以我認為我想要的方式是不可能的,所以我可以創建一段時間然后創建對象?

最好的辦法是將列表正確識別為字符串列表,並且不要使用原始列表。

由於拆分的結果是一個字符串數組,因此您必須將其包裝為一個列表。

然后您將擁有一個列表列表,為此使用 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());

您的示例的主要問題是您存儲了 arrays 對象的原始列表。 因此,當您將 toString() 調用到數組中時,您會得到以方括號開頭的 hashReference。

希望這對你有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM