You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求将Java 8 Lambda实现的PriorityQueue转换为Java 7代码

Converting Java 8 Lambda PriorityQueue Initialization to Java 7

Got it, let's get this sorted for Java 7! Since Java 7 doesn't support lambda expressions, we just need to replace that lambda with an anonymous inner class that implements the Comparator<Object> interface. Here's the equivalent code for your PriorityQueue initialization:

open = new PriorityQueue<Object>(new Comparator<Object>() {
    @Override
    public int compare(Object o1, Object o2) {
        Cell c1 = (Cell) o1;
        Cell c2 = (Cell) o2;
        // Paste your original lambda comparison logic here
        // Example: If you were comparing integer priorities, it might look like:
        // return Integer.compare(c1.getPriority(), c2.getPriority());
        // Or whatever custom ordering logic you had in your original lambda
    }
});

Quick Tips:

  • Java 7 requires explicit implementation of the Comparator interface instead of using lambdas. This anonymous inner class does exactly what your lambda was doing—defining how two Cell objects (cast from the Object parameters) should be ordered in the PriorityQueue.
  • If your original lambda used variables from the surrounding scope, remember that in Java 7 those variables must be marked final (Java 7 doesn't support the "effectively final" behavior introduced in Java 8). So double-check any captured variables and add the final modifier if needed.

内容的提问来源于stack exchange,提问作者Vladislav Varadinov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:30:57