您好我想修改此代码。我不想使用收藏家。我该怎么做
Files.lines(Paths.get("tekst.txt"))
.map(line -> line.toLowerCase().replaceAll("\\s+", " "))
.flatMap(line -> Arrays.stream(line.split("[\\.!?]")))
.map(sentence -> Arrays.stream(sentence.split(" "))
.filter(word -> unicodeWord.matcher(word).matches())
.map(Word::new)
.collect(Collectors.toList())
)
.filter(list -> list.size()>0)
.map(Sentence::new)
.map(converter::convert)
.forEach(System.out::print);
和
class CSVConverter implements Converter{
@Override
public String convert(Sentence sentence){
return sentence.getWords().stream().map(Object::toString).collect(Collectors.joining(","))+"\n";
答案 0 :(得分:0)
在第一个,您可以这样做:
Arrays.asList(Arrays.stream(sentence.split(" "))
.filter(word -> unicodeWord.matcher(word).matches())
.map(Word::new)
.toArray(Word[]::new))
在第二个:
StringBuilder sb = new StringBuilder();
sentence.getWords().stream().map(Object::toString).forEach(s -> sb.append(s).append(','));
return sb.setCharAt(sb.length() - 1, '\n').toString();
第二个没有Stream API:
StringBuilder sb = new StringBuilder();
for (Word word : sentence.getWords()) {
String s = Object.toString(word);
sb.append(s).append(',');
}
return sb.setCharAt(sb.length() - 1, '\n').toString();