collectAndThen方法是否足够有效? [英] Is collectingAndThen method enough efficient?

查看:1602
本文介绍了collectAndThen方法是否足够有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用collectAndThen,发现与其他编码程序相比需要花费一些时间,我用它来执行类似的任务。

I have recently started using collectingAndThen and found that it is taking a bit long time comparatively to the other coding procedures, which i used for performing the similar tasks.

这是我的代码:

        System.out.println("CollectingAndThen");
        Long t = System.currentTimeMillis();
        String personWithMaxAge = persons.stream()
                                        .collect(Collectors.collectingAndThen(
                                                                Collectors.maxBy(Comparator.comparing(Person::getAge)),
                                                                (Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none"
                                                ));


        System.out.println("personWithMaxAge - "+personWithMaxAge + " time taken = "+(System.currentTimeMillis() - t));
        Long t2 = System.currentTimeMillis();
        String personWithMaxAge2 = persons.stream().sorted(Comparator.comparing(Person::getAge).reversed())
                                                    .findFirst().get().name;
        System.out.println("personWithMaxAge2 : "+personWithMaxAge2+ " time taken = "+(System.currentTimeMillis() - t2));

此处输出:

CollectingAndThen
personWithMaxAge - Peter time taken = 17
personWithMaxAge2 : Peter time taken = 1

这表明collectAndThen相对花费的时间更长。

which is showing that collectingAndThen is taking a greater time comparatively.

所以我的问题是 - 我应该继续收集和继续是其他一些建议吗?

So my question is - Should I keep on collectingAndThen or there is some other suggestions?

推荐答案

collectAndThen 添加一个动作只是在集合结束时执行。

collectingAndThen adds a single action that is just performed at the end of the collection.

所以

String personWithMaxAge = persons.stream()
    .collect(Collectors.collectingAndThen(
        Collectors.maxBy(Comparator.comparing(Person::getAge)),
        (Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none"
    ));

Optional<Person> p = persons.stream()
    .collect(Collectors.maxBy(Comparator.comparing(Person::getAge)));
String personWithMaxAge = p.isPresent() ? p.get().getName() : "none";

在收集器中指定操作的实际优势显示当您使用生成的收集器作为另一个收集器的输入时收集,例如 groupingBy(f1,collectingAndThen(collector,f2))

The actual advantage of specifying the action within a collector shows when you use the resulting collector as input to another collect, e.g. groupingBy(f1, collectingAndThen(collector, f2)).

因为这是一次只执行一次的简单操作最后,对性能没有影响。此外,基于排序的解决方案不太可能比任何非平凡输入的 maxBy 操作更快。

Since this is a single trivial action performed exactly once at the end, there is no impact on the performance. Further, it’s unlikely that a sorted based solution is faster than a maxBy operation for any nontrivial input.

您只是使用破坏的基准测试,违反了如何在Java中编写正确的微基准测试?。最值得注意的是,您正在测量Stream框架第一次使用的初始初始化开销。只是交换两个操作的顺序将给你一个完全不同的结果。

You are simply using a broken benchmark violating several of the rules listed in "How do I write a correct micro-benchmark in Java?". Most notably, you are measuring the initial initialization overhead of the first usage of the Stream framework. Just swapping the order of the two operations will give you an entirely different result.

但是,没有必要使操作不必要地复杂化。如上所述,镜像现有Stream操作的收集器的优点是它们可以与其他收集器组合。如果不需要这样的组合,只需使用直接代码

Still, there is no need to make an operation unnecessarily complicated. As said, the advantage of collectors mirroring existing Stream operations is that they can be combined with other collectors. If no such combining is need, just use the straight-forward code

String personWithMaxAge = persons.stream()
    .max(Comparator.comparing(Person::getAge))
    .map(Person::getName).orElse("none");

这比收集器使用更简单,效率高于 sorted 的解决方案。

which is simpler than the Collector usage and more efficient than the sorted based solution.

这篇关于collectAndThen方法是否足够有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆