使用以下Java代码编辑数据,结果如下所示:
[Aba , heim , 2001436]
[Eler , Nord , 2002696]
[Abe , walde , 1999297]
[Mann , Bor , 2005161]
不幸的是,这无助于我进一步发展,我想更改输出,使其看起来像这样
Aba ~ heim ~ 2001436
Eler ~ Nord ~ 2002696
Abe ~ walde ~ 1999297
Mann ~ Bor ~ 2005161
这是代码
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Test {
public static <R> Stream<R> streamOf(Iterator<R> it) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false);
}
public static <R> Stream<R> reverse(Stream<R> s) {
return streamOf(s.collect(Collectors.toCollection(ArrayDeque::new)).descendingIterator());
}
public static String[] reorder(String[] row) {
int skip = row.length > 2 ? 2 : 1;
String last = reverse(Stream.of(row).skip(skip).map(s -> s.toLowerCase().startsWith("jg.") ? s.substring(3) : s)
.map(String::trim).filter(s -> !s.isEmpty()).filter(x -> Character.isDigit(x.charAt(0))))
.collect(Collectors.joining());
String[] result = new String[3];
result[0] = row[0];
result[2] = last.isEmpty() ? null : last;
if (skip == 2) {
result[1] = row[1];
}
return result;
}
public static void main(String[] args) throws Exception {
String str = "Aba ~ heim ~ 436~Jg.2001\n" + "Abe ~ Nord ~ 696 ~Jg.2002\n" + "Eler ~ walde ~297~Jg.1999\n"
+ "Mann ~ Bor~ 161~Jg.2005\n";
new BufferedReader(new StringReader(str)).lines().filter(l -> !l.isEmpty()).map(l -> l.split("~"))
.map(Test::reorder).map(Arrays::toString).forEach(System.out::println);
}
}
谢谢您的帮助
答案 0 :(得分:1)
更改此行:
new BufferedReader(new StringReader(str)).lines().filter(l -> !l.isEmpty()).map(l -> l.split("~"))
.map(Test::reorder).map(Arrays::toString).forEach(System.out::println);
到
new BufferedReader(new StringReader(str)).lines().filter(l -> !l.isEmpty()).map(l -> l.split("~"))
.map(Test::reorder).map(l->String.join("~", l)).forEach(System.out::println);