英文:
How to rewrite a method using Stream api?
问题
private static List<Transaction> loadTransactionFromFile() throws ParseException, IOException {
List<Transaction> transactions = new ArrayList<>();
List<String> lines = Files.readAllLines(Paths.get(MOVEMENT_LIST.trim()));
lines.remove(0);
transactions = lines.stream()
.map(line -> line.split(","))
.filter(fragments -> fragments.length == 8)
.map(fragments -> {
try {
return new Transaction(
fragments[ACCOUNT_TYPE],
fragments[ACCOUNT_NUMBER],
fragments[CURRENCY],
new SimpleDateFormat(dateFormat).parse(fragments[DATE_OF_OPERATION].trim()),
fragments[RRN],
fragments[INFO].replaceAll("\\d|\\\\+|\\+|RUR|MCC|(Apple Pay-)|\\.|\\)|\\(|RUSMOSCOW ?|MOSCOW|RUS|J|\\/RU\\/"", "").trim(),
Double.parseDouble(fragments[INCOME]),
Double.parseDouble(fragments[EXPENSE])
);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
})
.filter(transaction -> transaction != null)
.collect(Collectors.toList());
return transactions;
}
英文:
How to rewrite a method using Stream api?
private static List<Transaction> loadTransactionFromFile () throws ParseException, IOException {
List<Transaction> transactions = new ArrayList<>();
List<String> lines = Files.readAllLines(Paths.get(MOVEMENT_LIST.trim()));
lines.remove(0);
for (String line : lines) {
String[] fragments = line.split(",");
if (fragments.length != 8) {
// System.out.println("Wrong line:s " + line);
continue;
}
transactions.add(new Transaction(
fragments[ACCOUNT_TYPE],
fragments[ACCOUNT_NUMBER],
fragments[CURRENCY],
(new SimpleDateFormat(dateFormat)).parse(fragments[DATE_OF_OPERATION]
.trim()),
fragments[RRN],
fragments[INFO].replaceAll("\\d|\\\\+|\\+|RUR|MCC|(Apple Pay-)" +
"|\\.|\\)|\\(|RUSMOSCOW ?|MOSCOW|RUS|J|\\/RU\\/", "")
.trim(),
Double.parseDouble(fragments[INCOME]),
Double.parseDouble(fragments[EXPENSE])
));
}
return transactions;
}
答案1
得分: 2
我还没有进行过测试,但应该是类似以下这样的:
return lines.stream()
.skip(1)
.map(line -> line.split(","))
.filter(l -> l.length == 8)
.map(fragments -> new Transaction(
fragments[ACCOUNT_TYPE],
fragments[ACCOUNT_NUMBER],
fragments[CURRENCY],
(new SimpleDateFormat(dateFormat)).parse(fragments[DATE_OF_OPERATION].trim()),
fragments[RRN],
fragments[INFO].replaceAll("\\d|\\+|\\+|RUR|MCC|(Apple Pay-)|\\.|\\)|\\(|RUSMOSCOW ?|MOSCOW|RUS|J|\\/RU\\/", "").trim(),
Double.parseDouble(fragments[INCOME]),
Double.parseDouble(fragments[EXPENSE])
))
.collect(Collectors.toList());
英文:
I haven't tested but it should be something like this:
return lines.stream()
.skip(1)
.map(line -> line.split(","))
.filter(l -> l.length==8)
.map(fragments -> new Transaction(
fragments[ACCOUNT_TYPE],
fragments[ACCOUNT_NUMBER],
fragments[CURRENCY],
(new SimpleDateFormat(dateFormat))
.parse(fragments[DATE_OF_OPERATION]
.trim()),
fragments[RRN],
fragments[INFO].replaceAll("\\d|\\\\+|\\+|RUR|MCC|(Apple Pay-)" +
"|\\.|\\)|\\(|RUSMOSCOW ?|MOSCOW|RUS|J|\\/RU\\/", "")
.trim(),
Double.parseDouble(fragments[INCOME]),
Double.parseDouble(fragments[EXPENSE]))
.collect(Collectors.toList());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论