如果使用Java 8,并且搜索有可能返回null,则可以尝试使用 Optional类。要查找证章:private final Optional<Carnet> findCarnet(Collection<Carnet> yourList, String codeIsin){ // This stream will simply return any carnet that matches the filter. It will be wrapped in a Optional object. // If no carnets are matched, an "Optional.empty" item will be returned return yourList.stream().filter(c -> c.getCodeIsin().equals(codeIsin)).findAny();}现在,它的用法是:public void yourMethod(String codeIsin){ List<Carnet> listCarnet = carnetEJB.findAll(); Optional<Carnet> carnetFound = findCarnet(listCarnet, codeIsin); if(carnetFound.isPresent()){ // You use this ".get()" method to actually get your carnet from the Optional object doSomething(carnetFound.get()); } else{ doSomethingElse(); }}