Solved

List to Map

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Options in Zimbra Server 2 54
Spring Framework HTTPSession management 1 36
Apps blocked by Java 9 81
spring jars download 1 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

823 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