如何使用 Stream API 重写一个方法?

huangapple go评论72阅读模式
英文:

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\\/&quot;", "").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&lt;Transaction&gt; loadTransactionFromFile () throws ParseException, IOException {

    List&lt;Transaction&gt; transactions = new ArrayList&lt;&gt;();

    List&lt;String&gt; lines = Files.readAllLines(Paths.get(MOVEMENT_LIST.trim()));
    lines.remove(0);

    for (String line : lines) {
        String[] fragments = line.split(&quot;,&quot;);

        if (fragments.length != 8) {
            //  System.out.println(&quot;Wrong line:s &quot; + 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(&quot;\\d|\\\\+|\\+|RUR|MCC|(Apple Pay-)&quot; +
                        &quot;|\\.|\\)|\\(|RUSMOSCOW ?|MOSCOW|RUS|J|\\/RU\\/&quot;, &quot;&quot;)
                        .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 -&gt; line.split(&quot;,&quot;))
    .filter(l -&gt; l.length==8)
    .map(fragments -&gt; new Transaction(
            fragments[ACCOUNT_TYPE],
            fragments[ACCOUNT_NUMBER],
            fragments[CURRENCY],
            (new SimpleDateFormat(dateFormat))
                    .parse(fragments[DATE_OF_OPERATION]
                    .trim()),
            fragments[RRN],
            fragments[INFO].replaceAll(&quot;\\d|\\\\+|\\+|RUR|MCC|(Apple Pay-)&quot; +
                    &quot;|\\.|\\)|\\(|RUSMOSCOW ?|MOSCOW|RUS|J|\\/RU\\/&quot;, &quot;&quot;)
                    .trim(),
            Double.parseDouble(fragments[INCOME]),
            Double.parseDouble(fragments[EXPENSE]))
    .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年8月23日 17:00:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63545162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定