如何拆分字符串流并生成字符串数组列表?
How to split a stream of Strings and generate a List of String-arrays?
我对流很陌生,所以请帮助我(并且要温柔)。
我想做的是以下内容。我有一个从文件中读取的 BufferedReader
,其中每一行看起来像这样:"a, b"。例如:
示例输入文件
"a, b"
"d, e"
"f, g"
我想将其转换为 LinkedList<String[]>
:
例子LinkedList<String[]>
[{"a", "b"}, {"c", "d"}, {"f", "g"}]
您将如何使用流方法执行此操作?
这是我试过的:
List numbers = reader.lines().map(s -> s.split("[\W]")).collect(Collectors.toList());
这不起作用。我的 IDE 提供了以下反馈:
Incompatible types. Required List but 'collect' was inferred to R: no instance(s) of type variable(s) T exist so that List<T> conforms to List
它显示...我仍在尝试找出流。
你可以这样做,
Path path = Paths.get("src/main/resources", "data.txt");
try (Stream<String> lines = Files.lines(path)) {
List<String> strings = lines.flatMap(l -> Arrays.stream(l.split(","))).map(String::trim)
.collect(Collectors.toCollection(LinkedList::new));
}
读取文件的每一行,然后使用分隔符将其拆分。之后 trim 它会消除任何剩余的白色 space 字符。最后将其收集到结果容器中。
首先,我建议避免使用原始类型,而是使用 List<String[]>
作为接收者类型。
List<String[]> numbers = reader.lines()
.map(s -> s.split(delimiter)) // substitute with your deilimeter
.collect(Collectors.toList());
您提到您想要 LinkedList
实施。你应该几乎总是喜欢 ArrayList
默认情况下 toList
returns currently 尽管不能保证持续存在,但你可以明确指定列表实现toCollection
:
List<String[]> numbers = reader.lines()
.map(s -> s.split(delimiter)) // substitute with your deilimeter
.collect(Collectors.toCollection(ArrayList::new));
LinkedList
也是如此:
List<String[]> numbers = reader.lines()
.map(s -> s.split(delimiter)) // substitute with your deilimeter
.collect(Collectors.toCollection(LinkedList::new));
假设每一行都是 2 个元素的元组,您可以将它们收集到一个看起来像 2 个元素的元组的列表中。
请注意,Java 没有元组的原生类型(如 Scala 或 python),因此您应该选择一种方式来表示元素。
您可以创建条目列表:
List<Map.Entry<String, String>> numbers =
reader.lines()
.map(s -> s.split(","))
.map(a -> new AbstractMap.SimpleEntry<>(a[0], a[1]))
.collect(Collectors.toList());
或字符串列表:
List<String> numbers = reader.lines()
.map(s -> s.split(","))
.map(a -> "{" + a[0] + "," + a[1] + "}"))
.collect(Collectors.toList());
请注意,通常情况下,您不想在收集流时坚持使用特定的列表实现,但在某些情况下您可能需要。在这种情况下,指定要使用的集合供应商
toCollection(LinkedList::new)
而不是 toList()
我对流很陌生,所以请帮助我(并且要温柔)。
我想做的是以下内容。我有一个从文件中读取的 BufferedReader
,其中每一行看起来像这样:"a, b"。例如:
示例输入文件
"a, b"
"d, e"
"f, g"
我想将其转换为 LinkedList<String[]>
:
例子LinkedList<String[]>
[{"a", "b"}, {"c", "d"}, {"f", "g"}]
您将如何使用流方法执行此操作?
这是我试过的:
List numbers = reader.lines().map(s -> s.split("[\W]")).collect(Collectors.toList());
这不起作用。我的 IDE 提供了以下反馈:
Incompatible types. Required List but 'collect' was inferred to R: no instance(s) of type variable(s) T exist so that List<T> conforms to List
它显示...我仍在尝试找出流。
你可以这样做,
Path path = Paths.get("src/main/resources", "data.txt");
try (Stream<String> lines = Files.lines(path)) {
List<String> strings = lines.flatMap(l -> Arrays.stream(l.split(","))).map(String::trim)
.collect(Collectors.toCollection(LinkedList::new));
}
读取文件的每一行,然后使用分隔符将其拆分。之后 trim 它会消除任何剩余的白色 space 字符。最后将其收集到结果容器中。
首先,我建议避免使用原始类型,而是使用 List<String[]>
作为接收者类型。
List<String[]> numbers = reader.lines()
.map(s -> s.split(delimiter)) // substitute with your deilimeter
.collect(Collectors.toList());
您提到您想要 LinkedList
实施。你应该几乎总是喜欢 ArrayList
默认情况下 toList
returns currently 尽管不能保证持续存在,但你可以明确指定列表实现toCollection
:
List<String[]> numbers = reader.lines()
.map(s -> s.split(delimiter)) // substitute with your deilimeter
.collect(Collectors.toCollection(ArrayList::new));
LinkedList
也是如此:
List<String[]> numbers = reader.lines()
.map(s -> s.split(delimiter)) // substitute with your deilimeter
.collect(Collectors.toCollection(LinkedList::new));
假设每一行都是 2 个元素的元组,您可以将它们收集到一个看起来像 2 个元素的元组的列表中。 请注意,Java 没有元组的原生类型(如 Scala 或 python),因此您应该选择一种方式来表示元素。
您可以创建条目列表:
List<Map.Entry<String, String>> numbers =
reader.lines()
.map(s -> s.split(","))
.map(a -> new AbstractMap.SimpleEntry<>(a[0], a[1]))
.collect(Collectors.toList());
或字符串列表:
List<String> numbers = reader.lines()
.map(s -> s.split(","))
.map(a -> "{" + a[0] + "," + a[1] + "}"))
.collect(Collectors.toList());
请注意,通常情况下,您不想在收集流时坚持使用特定的列表实现,但在某些情况下您可能需要。在这种情况下,指定要使用的集合供应商
toCollection(LinkedList::new)
而不是 toList()