使用以下 java 代码编辑数据,结果如下所示[Aba , heim , 2001436][Eler , Nord , 2002696][Abe , walde , 1999297][Mann , Bor , 2005161] 不幸的是,这对我没有帮助,我想更改输出,使其看起来像这样Aba ~ heim ~ 2001436Eler ~ Nord ~ 2002696Abe ~ walde ~ 1999297Mann ~ 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); }}
1 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
更改此行:
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);
添加回答
举报
0/150
提交
取消