Java从列表中获取特定值并添加到其他列表

标签 java spring list hibernate rest

我想问一些问题,如何从列表中获取特定值并将其添加到另一个列表,假设我有列表(这是来自 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/

相关文章:

java - 根据元素的值对列表进行排序

java - 在用户存储库中使用安全注释时的 spring-data-rest 和 spring security 循环引用

java - Firefox WebDriver 在调用 findElement() 时不会抛出 NoElementFoundException 而是挂起?

java - org.hibernate.HibernateException : Current transaction is not in progress after migrate to Hibernate 5

spring - 更改现有队列的参数

java - 以编程方式使用 Spring 调度作业(动态设置 fixedRate)

java - vaadin中的文件上传和下载

python - 如何将列表分组为具有四列的数据框?

java - 禁止向 JTextField 输入一些符号

python - 测试值是否存在于多个列表中