Solved

List to Map

Posted on 2014-10-23
2
122 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 27

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 63
java stored proc example 9 45
hibernate example for saving data 19 55
Why doesn't this text field show up on my Applet frame? 2 20
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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