我该如何更改代码

时间:2015-03-09 20:41:14

标签: java

您好我想修改此代码。我不想使用收藏家。我该怎么做

 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";

1 个答案:

答案 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();