有没有办法使用Java 8中的数据流将特定字符描述的许多多行字符串收集到Arraylist中?

问题描述 投票:3回答:1

我有一个fasta文件,我想解析成ArrayList,每个位置都有一个完整的序列。序列是多行字符串,我不想在我存储的字符串中包含标识行。 我当前的代码将每一行拆分为ArrayList中的另一个位置。如何使每个位置由>字符描绘?

fasta文件的格式如下:

>identification of a sequence 1
line1
line3
>identification of a sequence 2
line4
>identification of a sequence 3
line5
line6
line7
public static void main(String args[]) {

        String fileName = "fastafile.fasta";
        List<String> list = new ArrayList<>();

        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

            //1. filter line 3
            //2. convert all content to upper case
            //3. convert it into a List
            list = stream
                    .filter(line -> !line.startsWith(">"))
                    .map(String::toUpperCase)
                    .collect(Collectors.toList());

        } catch (IOException e) {
            e.printStackTrace();
        }

        list.forEach(System.out::println);


    }

对于上面的示例,我们需要一个输出,以便:

System.out.println(list.size()); // this would be 3

System.out.println(list.get(0)); //this would be line1line3

System.out.println(list.get(1)); //this would be line4

System.out.println(list.get(2)); //this would be line5line6line7
arraylist collections java-8 fasta multilinestring
1个回答
1
投票

根据你的目标,使用Files.lines似乎会让事情变得有点棘手。

假设您可以简单地在单个String中获取文件的全部内容 - 以下工作非常好(使用online compiler验证):

import java.util.*;
import java.util.stream.*;


public class Test {
   public static void main(String args[]) {
     String content = ">identification of a sequence 1\n" +
        "line1\n" +
        "line3\n" +
        ">identification of a sequence 2\n" +
        "line4\n" +
        ">identification of a sequence 2\n" +
        "line5\n" +
        "line6\n" +
        "line7";
     List<String> list = new ArrayList<>();
     try {
        list = Arrays.stream(content.split(">.*"))
          .filter(e -> !e.isEmpty())
          .map(e -> e.replace("\n","").trim())
          .collect(Collectors.toList());
     } catch (Exception e) {
         e.printStackTrace();
     }

     list.forEach(System.out::println);

     System.out.println(list.size()); // this would be 3

     System.out.println(list.get(0)); // this would be line1line3

     System.out.println(list.get(1)); // this would be line4

     System.out.println(list.get(2)); // this would be line5line6line7

   }
}

输出是:

line1line3
line4
line5line6line7
3
line1line3
line4
line5line6line7
© www.soinside.com 2019 - 2024. All rights reserved.