繁体   English   中英

Java功能接口说明-传递给lambda表达式的参数

[英]Java Functional Interface clarification - Parameters passed into lambda expression

我有以下代码难以理解。 我有一个接口声明如下:

public interface WeightedRelationshipConsumer {
    boolean accept(int sourceNodeId, int targetNodeId, long relationId, double weight);
}

然后,我有第二个接口,它接受如下所示的WeightedRelationshipConsumer

public interface WeightedRelationshipIterator {
    void forEachRelationship(int nodeId, Direction direction, WeightedRelationshipConsumer consumer);
}

然后,在实现Dijkstra算法的过程中,我有以下代码:

private void run(int goal, Direction direction) {
        // `queue` is a Priority Queue that contains the vertices of the graph
        while (!queue.isEmpty()) {
            int node = queue.pop();
            if (node == goal) {
                return;
            }
            // `visited` is a BitSet.
            visited.put(node);
            // Gets the weight of the distance current node from a node-distance map.
            double costs = this.costs.getOrDefault(node, Double.MAX_VALUE);
            graph.forEachRelationship(
                    node,
                    direction, (source, target, relId, weight) -> {
                        updateCosts(source, target, weight + costs);
                        if (!visited.contains(target)) {
                            queue.add(target, 0);
                        }
                        return true;
                    });
        }
    }

这是

graph.forEachRelationship(node, direction, (source, target, relId, weight) -> {
   updateCosts(source, target, weight + costs);
   if (!visited.contains(target)) {
       queue.add(target, 0);
   }
   return true;
});

这让我感到困惑。 具体来说, sourcetargetrelIdweightrelId ,以及它们如何解析? 这4个变量未在此类中的其他任何地方定义。 updateCosts()如下:

private void updateCosts(int source, int target, double newCosts) {
    double oldCosts = costs.getOrDefault(target, Double.MAX_VALUE);
    if (newCosts < oldCosts) {
        costs.put(target, newCosts);
        path.put(target, source);
    }
}

另外,如果有可能有助于理解此类代码的资源,请提供。 谢谢。

您的界面似乎是一个功能性界面:

interface WeightedRelationshipConsumer {
  boolean accept(int sourceNodeId, int targetNodeId, long relationId, double weight);
}

这些也称为SAM类型(单一抽象方法类型),它们是使用lambda表达式或方法引用实现的候选对象。

Lambda表达式是一种实现接口唯一方法的方法。

例如

WeightedRelationshipConsumer wrc = (source, target, relId, weight) -> true;

这是为其实现accept方法提供实现的一种方式,其中(source, target, relId, weight)对应于该方法的声明参数,而其中true ,即lambda表达式的返回值,对应于该方法的返回类型。以及accept方法。

看来您的graph.forEachRelationship方法接受WeightedRelationshipConsumer的实例作为其第三个参数,因此,您可以传递一个lambda表达式作为参数。

正如您所问的情况一样:

graph.forEachRelationship(node, direction, (source, target, relId, weight) -> {
   updateCosts(source, target, weight + costs);
   if (!visited.contains(target)) {
       queue.add(target, 0);
   }
   return true;
});

关于参数的明显缺乏定义,这只是您的困惑。 Lambda表达式支持类型推断,因此,我们不需要再次提供参数的类型,毕竟,它们已经在lambda表达式实现的方法的签名中进行了声明(即accept )。

因此,之前的lambda可以声明为:

WeightedRelationshipConsumer wrc = (int sourceNodeId, int targetNodeId, long relationId, double weight) -> true

但是习惯上省略类型以使其更具可读性。 毕竟,编译器可以从accept的方法签名中推断出参数的类型。

因此,lambda括号内的标识符列表实际上是该函数的参数声明。

在Stackoverflow中, java-8标记下有大量参考资料。

暂无
暂无

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

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