【问题标题】:lambda expression not supporting in stream().allMatch() / .anyMatch() method [duplicate]lambda 表达式在 stream().allMatch() / .anyMatch() 方法中不支持 [重复]
【发布时间】:2020-12-14 18:40:55
【问题描述】:

我不明白为什么 JAVA 1.8 不支持 stream().filter() / allMatch() / anyMatch() 中的 lambda 表达式

例如:

ERROR snippet from eclipse IDE

import java.util.*; 
  
class GFG { 
      
    // Driver code 
    public static void main(String[] args) { 
          
    // Creating a list of Integers 
    List<Integer> list = Arrays.asList(3, 4, 6, 12, 20); 
      
    // Check if all elements of stream 
    // are divisible by 3 or not using  
    // Stream allMatch(Predicate predicate) 
    boolean answer = list.stream().allMatch(n-> n % 3 ==0); 
      
    // Displaying the result 
    System.out.println(answer); 
} 
} 

我在 Eclipse 中遇到错误,如“令牌“-”、-- 预期的语法错误和“n”下方的红线。 请帮助我理解并解决此问题。

#注意:我使用的是 eclipse 3.8、JAVA 8 (1.8.0_271)

【问题讨论】:

  • Eclipse 3.8 是古老的(8 年和 15 个旧版本)并且不理解 lambda 表达式,请使用现代版本的 Eclipse,例如当前的 2020-09。
  • 感谢您的建议。我会用最新的。

标签: java eclipse lambda java-8


【解决方案1】:

您的代码似乎在https://www.tutorialspoint.com/compile_java_online.php 中编译和运行 它运行 Java 1.8.0_141。 检查eclipse中的目标运行时间是否设置为java 8。

您可以以Target JRE in Eclipse 为例。

【讨论】:

  • 问题在于 Eclipse 内置的 Java 编译器 ('ecj')。 Eclipse 3.8 中的版本太旧了,无法理解 lambda 表达式。
猜你喜欢
  • 2017-06-08
  • 2015-04-29
  • 2016-02-29
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多