Why iterating is iterating the list only one time?

Tani :

I have one Bean named ProductBean and I have two list that contains product id and product size. Iam trying to get product size on the basis of id which is working fine but list contains two items and it is giving result for first item only. Code which I have tried below is-

Iterator itr = cart.iterator();
while (itr.hasNext()) {
    Integer i = (Integer) itr.next();    
    LOG.info("Poduct Id " + i);
    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery<ProductBean> citeriaQuery = builder.createQuery(ProductBean.class);
    Root<ProductBean> root = citeriaQuery.from(ProductBean.class);
    citeriaQuery.select(root);
    citeriaQuery.where(builder.equal(root.get("product_id"), i));
    Query<ProductBean> query = session.createQuery(citeriaQuery);
    productBeanList = query.list();                
    for (ProductBean productBean : productBeanList) {
        StringBuilder sb = new StringBuilder();
        sb.append(productBean.getProduct_size());
        String[] lines = sb.toString().split(", ");                    
        for (String s : lines) {                       
            itr = sizes.iterator();
            while (itr.hasNext()) {
                Object size = (Object) itr.next();                            
                if (String.valueOf(size).equals(s)) {
                    LOG.info("If condition matches " + String.valueOf(size) + " id is " + i);
                    productMap = new HashMap();
                    productMap.put("productBeanSize", String.valueOf(size));
                    productMap.put("productBean", productBean);
                    productList.add(productMap);
                }
            }
        }
    }
}
Selaron :

You are reassigning your Iterator itr in a confusing way in the inner for loop.

After the inner while loop fineshes, the itr is done and has no more elements, thus itr.hasNext() is false in the outer while too - as it is the same variable visible to all of the code you provided here.

Better create a new deeper scoped iterator variable in the inner loop:

// renamed itr to cartItr
// added final modifier to prevent re assignment.
final Iterator cartItr = cart.iterator();
while (cartItr.hasNext()) {
    Integer i = (Integer) cartItr.next();    
    LOG.info("Poduct Id " + i);
    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery<ProductBean> citeriaQuery = builder.createQuery(ProductBean.class);
    Root<ProductBean> root = citeriaQuery.from(ProductBean.class);
    citeriaQuery.select(root);
    citeriaQuery.where(builder.equal(root.get("product_id"), i));
    Query<ProductBean> query = session.createQuery(citeriaQuery);
    productBeanList = query.list();                
    for (ProductBean productBean : productBeanList) {
        StringBuilder sb = new StringBuilder();
        sb.append(productBean.getProduct_size());
        String[] lines = sb.toString().split(", ");                    
        for (String s : lines) {                       
            // added extra iterator variable sizesItr only visible in this for-block.
            // added final modifier to prevent re assignment.
            final Iterator sizesItr = sizes.iterator();
            while (sizesItr.hasNext()) {
                Object size = (Object) sizesItr.next();                            
                if (String.valueOf(size).equals(s)) {
                    LOG.info("If condition matches " + String.valueOf(size) + " id is " + i);
                    productMap = new HashMap();
                    productMap.put("productBeanSize", String.valueOf(size));
                    productMap.put("productBean", productBean);
                    productList.add(productMap);
                }
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=147932&siteId=1