这个lambda语法(Java)有什么问题?
只是为了让我不会忘记这个lambda语法(Java)有什么问题?,java,spring-boot,lambda,Java,Spring Boot,Lambda,只是为了让我不会忘记 Java8SE 2019-12月食 弹簧防尘套2.2.4.1释放 我正在努力学习SpringBoot,我正在努力理解为什么lambda不能编译。下面是spring引导示例(编译) 下面是我根据自己的需要修改此示例的尝试: @RestController public class SwitchController { @Autowired JdbcTemplate jdbcTemplate; @GetMapping public Rows getSwitchRows(
- Java8SE
- 2019-12月食
- 弹簧防尘套2.2.4.1释放
@RestController
public class SwitchController {
@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping
public Rows getSwitchRows() {
Rows switches = new Rows();
jdbcTemplate.query(
"SELECT swityp, oldkey, newkey, delete FROM table",
(rs, rowNum) -> new Switch(rs.getString("swityp"), rs.getString("oldkey"), rs.getString("newKey"), rs.getString("delete"))
).forEach(switch -> switches.addRow(switch));
return switches;
}
}
forEach()
中的lambda在->
上给出了一个错误,该错误表示令牌上的语法错误“->”(预期为,也在开关上。addRow(开关)
表示令牌“开关”上的语法错误,删除该令牌
。这几乎就像forEach()
不知道我输入了一个lambda。也许我认为是lambda,不是。如果是这样,是什么让它不是lambda
以下是我的开关
和行
类,以防有帮助:
Switch.java
public class Switch {
private final String switchType;
private final String oldKey;
private final String newKey;
private final String delete;
public Switch(String switchType, String oldKey, String newKey, String delete) {
this.switchType = switchType;
this.oldKey = oldKey;
this.newKey = newKey;
this.delete = delete;
}
public String getSwitchType() {
return switchType;
}
public String getOldKey() {
return oldKey;
}
public String getNewKey() {
return newKey;
}
public String getDelete() {
return delete;
}
}
Rows.java
public class Rows {
private Object[] rows;
public Rows () {
super();
}
public void addRow (Object row) {
rows[rows.length] = row;
}
}
switch
-关键字!您使用的是哪个IDE?是否在其中启用Lambda?swotch
是新的clazz
…或zwitch
顺便说一句,您的代码很好。@Iman:如上所述,Lambda不好,因为switch
在java中不是有效的变量名(它是关键字)。
public class Rows {
private Object[] rows;
public Rows () {
super();
}
public void addRow (Object row) {
rows[rows.length] = row;
}
}