我想问一些问题,如何从列表中获取特定值并将其添加到另一个列表,假设我有列表(这是来自 hibernate DAO 的 cust 列表),例如:
[["marketplace","001-002-003"],["insurance","142-523-132"],["car purchase","982349824"]]
我只想从该列表中获取“市场”、“保险”和“汽车购买”的值,并将其添加到名为“bu”的新列表中
这是我的代码
public @ResponseBody String findBU(@RequestBody AllCustomerHist customer){
BigDecimal id= customer.getId();
String message;
List<String> bu= new ArrayList<>();
int i;
System.out.println("ID = "+id);
List<AllCustomerHist> cust = allCustomerHistService.findBU(id);
for (i=0; i<cust.size(); i++){
System.out.println("iteration = "+i);
// stumbled here //
}
JSONObject json = new JSONObject();
json.put("id", id);
json.put("BU", bu);
message = json.toString();
return message;
}
这是我的 AllCustomerHistDaoImpl 类
//release 1.3
@SuppressWarnings("unchecked")
public List<AllCustomerHist> findBU(BigDecimal adpId) {
// TODO cek kodingan
Criteria criteria = getSession().createCriteria(AllCustomerHist.class)
.setProjection(Projections.projectionList()
.add(Projections.property("srctable"), "srctable")
.add(Projections.property("customerId"), "customerId"))
.add(Restrictions.eq("adpId", adpId));
return (List<AllCustomerHist>)criteria.list();
}
注意AllCustomerHist是一个实体类,用于在hibernate中定义表
谢谢你的帮助:D
最佳答案
由于您需要进行一些验证,并且需要删除整个 AllCustomerHist
对象,所以我要做的是以下代码
List<AllCustomerHist> cust = allCustomerHistService.findBU(id);
List<String> bu = new ArrayList<String>(cust.size());
for (i=0; i<cust.size(); i++){
System.out.println("iteration = "+i);
AllCustomerHist aCust = cust.get(i);
bu.add(aCust.getSrctable());
}
//here your bu list should be ready to be used.....
希望这是您所需要的
关于Java从列表中获取特定值并添加到其他列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873855/