Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

List to Map

hi experts

In my database table i have data like this

Product                 type              currency

IMAGE-CARTS        SamInc.               USD
IMAGE-CARTS        BCI                   USD
IMAGE-CARTS        RII                        USD
IMAGE-CARTS        POP                  USD

POP-CARTS        SamInc.               USD
POP-CARTS        BCI                         USD
POP-CARTS        RII                        USD
POP-CARTS        POP                        USD


BARREL-CARTS        SamInc.               USD
BARREL-CARTS        BCI                   USD
BARREL-CARTS        RII                        USD
BARREL-CARTS        POP                  USD


I am firing a sql and getting back above data as List<ProductVO> like
List<ProductVO> completeProductList = db.getData();

ProductVO  is a bean defined as
public class ProductVO {
public String product;
public String type;
public String currency;
}
set(); methods
get(); methods

Open in new window



my goal is to create a Map<String,List<ProductVO>> which contains data like this
Key                    value
IMAGE-CARTS     List<ProductVO>  -- the count of the list must be 4 (for each type and currency)
POP-CARTS       List<ProductVO>  -- the count of the list must be 4 (for each type and currency)
BARREL-CARTS    List<ProductVO>  -- the count of the list must be 4 (for each type and currency)


what i am doing is something like this

Map<String,List<ProductVO>> productMap = new HashMap<String,List<ProductVO>>();
List<ProductVO> tempList = new ArrayList<ProductVO>();

for (ProductVO productData: completeProductList){      //original list from query                  
                  String product = productData.getProduct();
                  tempList.add(productData);                  
                  productMap.put(product,tempList);  
            }
Now my productMap has 3 keys and each key has value as List<ProductVO> with count 12

Any idea how i can create a Map which contains  3 keys and each key has value as List<ProductVO> with count 4 ?
 
Thanks very much.
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

Thanks Doug. I appreciate your help.
Any help with my next question will be greatly appreciated.
https://www.experts-exchange.com/questions/28543903/HashMap-Converting-long-string-values-to-smaller-codes.html