这很好用:// ActorDaopublic Optional<Actor> read(long id) { return Optional.ofNullable(actors.get((int) id));}public List<Actor> readAll() { // Return a list of actors from db }// Demopublic static Actor getActor(String firstName, String lastName) { Dao<Actor> actorDao = new ActorDao(); long id = 0; for (Actor actor : actorDao.readAll()) { if (firstName.equalsIgnoreCase(actor.getFirstName()) && lastName.equalsIgnoreCase(actor.getLastName())) { id = actor.getId() - 1; // actor_id in db starts at 1 return actorDao.read(id).get(); } } return null;}Java8+ 中更优雅的方式是什么?
1 回答
月关宝盒
TA贡献1772条经验 获得超5个赞
return actorDao.readAll() .stream() .filter(actor -> firstName.equalsIgnoreCase(actor.getFirstName())) .filter(actor -> lastName.equalsIgnoreCase(actor.getLastName())) .map(actor -> actorDao.read(actor.getId() - 1).get()) .findFirst() .orElse(null);
添加回答
举报
0/150
提交
取消