search in java ArrayList

前端 未结 8 1880
别跟我提以往
别跟我提以往 2020-12-03 05:04

I\'m trying to figure out the best way to search a customer in an ArrayList by its Id number. The code below is not working; the compiler tells me that I am mis

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 06:10

    Personally I rarely write loops myself now when I can get away with it... I use the Jakarta commons libs:

    Customer findCustomerByid(final int id){
        return (Customer) CollectionUtils.find(customers, new Predicate() {
            public boolean evaluate(Object arg0) {
                return ((Customer) arg0).getId()==id;
            }
        });
    }
    

    Yay! I saved one line!

提交回复
热议问题