2017-08-25 87 views
1

我想问一些问题,如何从列表中获取特定值并添加到另一个列表中,可以说我有列表(这是来自hibernate DAO的cust列表),如:Java从列表中获取特定值并添加到其他列表

[["marketplace","001-002-003"],["insurance","142-523-132"],["car purchase","982349824"]] 

我只是想获得“市场”,“保险”,从该列表中的值,并且“买车”,并添加到一个名为“不”

这里的新名单是我的代码

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; 

    } 

this是我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是实体类来定义表处于休眠

感谢您的帮助:d

+0

什么'AllCustomerHist'的结构?更重要的是,为什么假设我们会知道? – shmosel

+0

为什么你使用ID作为BigDecimal? –

+0

你已经从Hibernate中看到了一个看起来像一个Map的cust列表。然后,您使用AllCustomerHist类型元素的列表。看起来它不兼容。 –

回答

1

既然你需要做一些验证,你需要起飞全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..... 

我希望这是你需要什么

+0

中添加setResultTransformer(Transformers.aliasToBean(AllCustomerHist.class))它不能从对象转换到allcustomerhist,您的代码是正确的,但我需要使用setResultTransformer转换我的结果集Transformers.aliasToBean(AllCustomerHis t.class)) – galih

+0

啊所以'AllCustomerHist'不是实体类....我明白这是实体类,否则我会告诉你使用休眠变压器。无论如何,我很高兴它的工作 –

+0

谢谢你的答案的人,真的appreaciate它! – galih

0

如果使用JDK1.8 +,你也可以做这样的:

List<AllCustomerHist> cust = allCustomerHistService.findBU(id); 
List<String> bu = cust.stream() 
    .map(AllCustomerHist::getSrctable) 
    .collect(Collectors.toList()); 
相关问题