我有一个方法getListOfTgs(),它也返回一个set. 我有一个方法getListOfClass(),它也返回一个set.

我try 使用forEach方法将它们组合在一起:

scraper.getListOfTags(globalDocument).forEach(tag -> tagsAndClasses.add(tag));
scraper.getListOfClasses(globalDocument).forEach(class -> tagsAndClasses.add(class));

enter image description here

On the second forEach, the class variable name gets underlined and I get this error :
Syntax error on token "class", invalid TypeElidedFormalParameter
When I change the variable name to e.g. classes the problem goes away.
Why is it that I cant name my variable "class"?

推荐答案

class是Java中的一个关键字.您不能将其用作变量名.

以下是其他关键字的列表,并更深入地研究它们的规则.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

这里有一个更新的名单--https://en.wikipedia.org/wiki/List_of_Java_keywords

EDIT-在这样的情况下,我喜欢用eachClass,或者类似的东西.

Java相关问答推荐

在Keycloak测试容器中的测试之间清理数据库

如何在Spring Boot中创建500错误的响应正文?

使用ExecutorService时在ThreadFactory中触发自定义newThread函数

Java 22模式匹配不适用于记录模式匹配.给出汇编问题

如何审查Java dtos中的自定义注释字段?

当我用OkHttpClient重写shouldInterceptRequest来发布数据时,Android WebView正在以纯HTML加载URL内容

尽管类型擦除,instanceof与泛型在Java中如何工作?

springboot start loge change

Java .类参数不通过构造函数传递

为什么我的ArrayList索引的索引总是返回-1?

如何才能使我的程序不会要求两次输入?

使用传递的参数构造异常的Mockito-doThrow(或thenThrow)

如何确定springboot在将json字段转换为Dto时如何处理它?

Spring @Value default无法计算表达式

SpringBoot Kafka自动配置-适用于SASL_PLAYTEXT的SSLBundle 包,带SCRAM-SHA-512

尽管通过中断请求线程死亡,但线程仍将继续存在

Lombok@Nonnull是否也对供应商有影响?

是否为计划任务补偿系统睡眠?

如何在@CsvSource中传递空格作为值

使IntelliJ在导入时优先 Select 一个类或将另一个标记为错误