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

标签 arraylist collections java-8 fasta multilinestring

我有一个 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

最佳答案

根据您的目标,使用 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

关于arraylist - 有没有办法使用Java 8中的数据流将由特定字符描绘的许多多行字符串收集到Arraylist中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55882646/

相关文章:

java - 我想使用 java 集合以加快处理速度但同时避免内存堆异常?

java - 如何从 Instant.now() 获取 DayOfWeek

Java 8 的流 : why parallel stream is slower?

java - 在 Java 中同时对三个 ArrayLists 进行排序的最有效方法是什么

scala - 当只有一个选项不为空时做某事

java - 'empty' 集合的目的是什么?

java - 如何从 Instant 和时间字符串构造 ZonedDateTime?

java - 在包含字符串的 ArrayList 中查找索引

Java 如何知道 arraylist 是否包含对象属性中的值

java - 如何从ArrayList中删除一个对象?