Solved

List to Map

Posted on 2014-10-23
2
117 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 26

Accepted Solution

by:
dpearson earned 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now