0

I have arrayList

ArrayList<Product> productList  = new ArrayList<Product>();
 productList  = getProducts();  //Fetch the result from db

This list is stored in an ArrayList. The problem is that when I print its data, I obtain :

A, Arizona, 1980
B, Arizona, 1970
C, NewYork, 1980
D, NewYork, 1970
E, California, 1960

I want to convert to ArrayList to Map based on area:

Map<Integer, Map<String, List<Product>>>//Integer here is year and String here is manufacturingArea

The Product bean has the following structure :

class Product{
    private String name;
    private String manufacturingArea;
    private int year;
    /* Getters and setters*/
    /* toString method */
}

I'd like to convert to map like this :

{1980= [Arizona,A], [NewYork,C]},
{1970= [NewYork,B],[NewYork,D]},
{1960= [California,E]}

how can I group the data by converting arraylist to map?

3
  • you have to map it maually Commented Apr 8, 2014 at 5:02
  • One loop over the array to create the map should do the trick. And another loop to print the map. What have you tried? Commented Apr 8, 2014 at 5:02
  • Your map declaration and your data do not match. Commented Apr 8, 2014 at 5:31

1 Answer 1

7

Maybe something like this?

Map<String, List<Product>> newMap = new HashMap<String, List<Product>>();

for (Product product: productList) {
   if (!newMap.containsKey(product.name)) 
        newMap.put(product.name, new ArrayList<Product>())
   newMap.get(product.name).add(product)
}

Per the update to the question, note that year is private, but I am assuming it is readable in the context. The following code is untested, but should get you pretty close.

Map<Integer, Map<String, List<Product>>> newMap = new HashMap<Integer, Map<String, List<<Product>>>();
for (Product product: productList) {
   if (!newMap.containsKey(product.year)) // Java should do auto-boxing here 
        newMap.put(product.year, new HashMap<String, Product>());
   if (!newMap.get(product.year).containsKey(product.manufacturingArea);
        newMap.get(product.year).put(product.manufacturingArea, new ArrayList<Product>());

   newMap.get(product.year).get(product.manufacturingArea).add(product));
}
Sign up to request clarification or add additional context in comments.

9 Comments

I've a feeling that the OP wants to know about how to print it in the format he/she wants rather than how to convert it to a Map. But, ofcourse, I may be wrong too!
@Ɍ.Ɉ, He removed the accept after I edited, so I guess he didn't want it :P
@merlin2011: I tried above code. HashMap replaces the previous value associated with the given key in the map (conceptually like an array indexing operation for primitive types).
@Unknown, checking this.
@merlin2011 - Honestly, I'd prefer you keep both the parts of the answer. Extra information is never harmful you see :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.