1

I'm working on an Android project in which I have to get all the values from a hash table regardless the key. My map is

HashMap<String, ArrayList<MyProduct>> orderAdap = InvocieProductFragment.mapOrderd;

And from this map I'm loading relevent Product ArrayList according to product name.

ArrayList<MyProduct> lstStyle = orderAdap.get(lstBrandCode.get(position));

But my problem is can I load all the Product lists into a single ArrayList without concerning key values in this hash map.

For instance imagine my hash map is something like this.

[ss,[p1,p2,p3]],[we,[p3,p4]],[rf,[p1,p4,p5,p7,p9]]

and I need the output as

[p1,p2,p3,p3,p4,p1,p4,p5,p7,p9]

Can this be done. Need help. Thanks.....

Samantha Withanage
  • 3,811
  • 10
  • 30
  • 59
  • possible duplicate of [Get value from map list](http://stackoverflow.com/questions/13679427/get-value-from-map-list) – Pankaj Kumar Nov 26 '13 at 09:35

6 Answers6

3

I have to get all the values from a hash table regardless the key

Use the values() method from map :

Map<String, List<Integer>> m = new HashMap<>();
m.put("test", new ArrayList<Integer>(Arrays.asList(1,2,3)));
m.put("test2", new ArrayList<Integer>(Arrays.asList(2,3,4)));

List<List<Integer>> l = new ArrayList<>(m.values());
System.out.println(l);

Output :

[[1, 2, 3], [2, 3, 4]]

EDIT : To have a flatten List

        List<Integer> flattenList = new ArrayList<>();
        for(List<Integer> list : m.values()){
            for(Integer i : list)
                flattenList.add(i);
        }
        System.out.println(flattenList);

Output :

[1, 2, 3, 2, 3, 4]

Demo here

Additional note : You can check this if you want to flatten a list of an arbitrary depth (using recursion)

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
2

But my problem is can I load all the Product lists into a single ArrayList without concerning key values in this hash map.

You are looking for the method values() on HashmMap

Returns a Collection view of the values contained in this map.

So it turns

Collection<ArrayList<MyProduct>> allValuesCollection = orderAdap.values();

Then you can iterate over it

for (ArrayList<MyProduct> eachList : allValuesCollection ) {
            //play with  eachList 
        }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

just iterate you map using value set and add to your list.

java.util.Iterator iter = hm.valueSet().iterator();

while(iter.hashNext()){

//add to your list
}
shreyansh jogi
  • 2,082
  • 12
  • 20
0

do like this

 Collection<ArrayList<MyProduct>> values = orderAdap.values();
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

You will have to iterate over values:: may be this will help: Iterate through a HashMap

for (Object value : map.values()) {
    // ... add values to lstStyle without worring about key
}

hope this helps!

Community
  • 1
  • 1
  • How can I add the product values in the hash map into an ArrayList. The product values in the hash map are stored in ArrayLists. So how can I get all the values in those ArrayLists into an ArrayList. – Samantha Withanage Nov 26 '13 at 10:10
0
        MyProduct myProduct1 = new MyProduct();
        myProduct1.setProductId(1);
        myProduct1.setProductName("asm");

        MyProduct myProduct2 = new MyProduct();
        myProduct2.setProductId(2);
        myProduct2.setProductName("tsm");

        MyProduct myProduct3 = new MyProduct();
        myProduct3.setProductId(2);
        myProduct3.setProductName("rsm");

        List<MyProduct> productList1 = new ArrayList<>();
        productList1.add(myProduct1);
        productList1.add(myProduct2);

        List<MyProduct> productList2 = new ArrayList<>();
        productList2.add(myProduct3);

        Map<String, List<MyProduct>> map = new HashMap<String, List<MyProduct>>();
        map.put("First", productList1);
        map.put("Second", productList2);


        Collection<List<MyProduct>> listOfList = map.values();

        for(List<MyProduct> list : listOfList){
            for(MyProduct product: list){
                System.out.println(""+ product.getProductName());   
            }

        }
Suvasis
  • 1,451
  • 4
  • 24
  • 42