简体   繁体   English

如何使用Java 8中的比较器按参数对流进行排序?

[英]How to sort a stream by parameter using a comparator in Java 8?

How to sort a collection using comparator and a parameter in Java 8? 如何使用Java 8中的比较器和参数对集合进行排序?

Here is a piece of code: 这是一段代码:

    List<Point> sortedNeurons = neurons.parallelStream()
        .sorted((n1, n2) -> Double.compare(
            n1.getEuclideanDistanceFrom(inputVector),
            n2.getEuclideanDistanceFrom(inputVector)))
        .collect(Collectors.toList());

You are given a parameter inputVector that can be passed to a function that returns a primitive double value. 您将获得一个参数inputVector,该参数可以传递给返回原始double值的函数。 If applied to an element of the collection, it returns some number. 如果应用于集合的元素,则返回一些数字。 I want the collection to be ordered by this value. 我希望通过此值对集合进行排序。 Something like: select id from neurons order by getEuclideanDistanceFrom(inputVector, id); 类似于:通过getEuclideanDistanceFrom(inputVector,id)从神经元中选择id;

There are three problems here: 这里有三个问题:

  1. nx.getEuclideanDistanceFrom(inputVector) gets repeated twice. nx.getEuclideanDistanceFrom(inputVector)重复两次。
  2. I would like to use natural ordering of a double type, without declaring it, just like in an SQL query, when using order by clause. 我想在使用order by子句时使用double类型的自然排序,而不是像在SQL查询中那样声明它。
  3. Perhaps n1, n2 -> n1, n2 could be substituted with a double colon :: notation. 也许n1,n2 - > n1,n2可以用双冒号::符号代替。

PS I have a strong feeling that it can be fixed using something like bifunction or biconsumer... but couldn't figure it out... PS我有一种强烈的感觉,它可以使用像双功能或双音素这样的东西来修复......但是无法弄清楚...

Maybe you can do it with Comparator#comparingDouble : 也许你可以用Comparator#comparingDouble来做到这一点:

List<Point> sortedNeurons = neurons.parallelStream()
    .sorted(Comparator.comparingDouble(p -> p.getEuclideanDistanceFrom(inputVector)))
    .collect(Collectors.toList());

This looks good. 这看起来不错。 I made it even better: 我做得更好:

List<Point> sortedNeurons = neurons.parallelStream()
    .sorted(Comparator.comparingDouble(inputVector::getEuclideanDistanceFrom))
    .collect(Collectors.toList());

and then I realized, you can make it even shorter: 然后我意识到,你可以做得更短:

List<Point> sortedNeurons =
neurons.sort(Comparator.comparingDouble(inputVector::getEuclideanDistanceFrom));

Apparently streams are not good for sorting things... 显然溪流不适合分类......

I also found a way to extract the comparator that can be reused: 我还找到了一种方法来提取可以重用的比较器:

public interface Functional<T> {
    public int compareByEuclideanDistance(T o1, T o2);
}

public class Point implements Functional<Point> {
    @Override
    public int compareByEuclideanDistance(Point o1, Point o2) {
        return Double.compare(this.getEuclideanDistanceFrom(o1),
            this.getEuclideanDistanceFrom(o2));
    }
}

Now you can make any call such as: 现在您可以拨打任何电话,例如:

neurons.parallelStream()
    .sorted(input::compareByEuclideanDistance)
    .collect(Collectors.toList());

or 要么

neurons.parallelStream().min(input::compareByEuclideanDistance).get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM