这很好用:
// ActorDao
public Optional<Actor> read(long id) {
return Optional.ofNullable(actors.get((int) id));
}
public List<Actor> readAll() {
// Return a list of actors from db
}
// Demo
public 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+ 中更优雅的方式是什么?
月关宝盒
相关分类