Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

List to Map

Posted on 2014-10-23
2
Medium Priority
?
127 Views
Last Modified: 2014-10-24
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.
0
Comment
Question by:royjayd
2 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 40401093
The problem you're having is that you're only creating 1 list.

You need to create 1 list for each key.

Something like this:

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

for (ProductVO productData: completeProductList){      //original list from query                  
                  String product = productData.getProduct();
                  List<ProductVO> list = productMap.get(product) ;

                  // Create a list for this key if one does not already exist
                  if (list == null) {
                       list = new ArrayList<ProductVO>() ;
                       productMap.put(product,list);  
                  }

                  // By this point the list is always in the map so we can just add to it
                  list.add(productData);                  
            }

Open in new window

0
 

Author Comment

by:royjayd
ID: 40401984
Thanks Doug. I appreciate your help.
Any help with my next question will be greatly appreciated.
http://www.experts-exchange.com/Programming/Languages/Java/Q_28543903.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question