1

我有一个 Order 对象列表-

class Order {
Date date;
float amount;
String companyCode;
}
List<Order> orders = /* Initialize with list of order objects with valid data */

我有一个公司对象列表-

class Company {
String name;
String code;
String address;
}
List<Company> companies = /* Initialize with list of company objects with valid data */

我需要一个创建公司代码和名称的地图。

是否有一些库可以让我编写这样的代码(其中 BeanSearch 是假设的库类)?

Map<String, String> codeAndName = new HashMap<String, String>();
for(Order o: orders) {
   codeAndName.put(o.getCompanyCode(), 
                   BeanSearch.find(companies, "code", o.getCompanyCode).getName());
}

或者还有另一种好方法吗?

4

1 回答 1

1

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html should work for you right? Specifically you can use the find method

于 2012-10-25T05:09:15.763 回答