如何通过属性在ArrayList中查找对象

我如何Carnet在ArrayList<Carnet>知道对象属性的情况下找到它codeIsin。


List<Carnet> listCarnet = carnetEJB.findAll();


public class Carnet {


    private String codeTitre;

    private String nomTitre;

    private String codeIsin;


    // Setters and getters


}


函数式编程
浏览 1799回答 3
3回答

慕尼黑的夜晚无繁华

您不能没有迭代。选项1Carnet findCarnet(String codeIsIn) {&nbsp; &nbsp; for(Carnet carnet : listCarnet) {&nbsp; &nbsp; &nbsp; &nbsp; if(carnet.getCodeIsIn().equals(codeIsIn)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return carnet;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return null;}选项2覆盖的equals()方法Carnet。选项3而是将您的存储List为密钥:MapcodeIsInHashMap<String, Carnet> carnets = new HashMap<>();// setting mapCarnet carnet = carnets.get(codeIsIn);

蛊毒传说

在Java8中,您可以使用流:public static Carnet findByCodeIsIn(Collection<Carnet> listCarnet, String codeIsIn) {&nbsp; &nbsp; return listCarnet.stream().filter(carnet -> codeIsIn.equals(carnet.getCodeIsin())).findFirst().orElse(null);}另外,如果您有许多不同的对象(不仅是Carnet),或者想通过不同的属性(不仅是通过cideIsin)找到它,则可以构建一个实用程序类,以将其逻辑封装在其中:public final class FindUtils {&nbsp; &nbsp; public static <T> T findByProperty(Collection<T> col, Predicate<T> filter) {&nbsp; &nbsp; &nbsp; &nbsp; return col.stream().filter(filter).findFirst().orElse(null);&nbsp; &nbsp; }}public final class CarnetUtils {&nbsp; &nbsp; public static Carnet findByCodeTitre(Collection<Carnet> listCarnet, String codeTitre) {&nbsp; &nbsp; &nbsp; &nbsp; return FindUtils.findByProperty(listCarnet, carnet -> codeTitre.equals(carnet.getCodeTitre()));&nbsp; &nbsp; }&nbsp; &nbsp; public static Carnet findByNomTitre(Collection<Carnet> listCarnet, String nomTitre) {&nbsp; &nbsp; &nbsp; &nbsp; return FindUtils.findByProperty(listCarnet, carnet -> nomTitre.equals(carnet.getNomTitre()));&nbsp; &nbsp; }&nbsp; &nbsp; public static Carnet findByCodeIsIn(Collection<Carnet> listCarnet, String codeIsin) {&nbsp; &nbsp; &nbsp; &nbsp; return FindUtils.findByProperty(listCarnet, carnet -> codeIsin.equals(carnet.getCodeIsin()));&nbsp; &nbsp; }}

阿波罗的战车

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

相关分类

Java