Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve all values from HashMap keys in an ArrayList Java

Good day, this is kind of confusing me now(brain freeze!) and seem to be missing something. Have an ArrayList which i populate with a HashMap. now i put in my HashMap and arraylist.

 Map.put(DATE, value1);  Map.put(VALUE, value2);   arraylist.put(Map); 

Since am parsing a JSON, the arraylist increases in significant size. now my question is how do you get the values from both map keys in the arraylist? i have tried this

  if(!list.isEmpty()){   // list is an ArrayList              for(int k = 0; k < list.size(); k++){                 map = (HashMap)list.get(k);             }          }          Log.d(TAG, "map size is" + map.size());          String [] keys = new String[map.size()];         String [] date_value = new String[map.size()];          String [] value_values = new String[map.size()];          int i = 0;         Set entries = map.entrySet();         Iterator iterator = entries.iterator();          while(iterator.hasNext()){              Map.Entry mapping = (Map.Entry)iterator.next();             keys[i] = mapping.getKey().toString();              date_value[i] = map.get(keys[i]);              if(keys[i].equals(DATE)){                 date_value[i] = map.get(keys[i]);              } else if(keys[i].equals(VALUE)){                 value_values[i] = map.get(keys[i]);             }                     i++;                  } 

But i can't seem to get all the values. the Map size always return a value of 2, which is just the elements. how can i get all the values from the Map keys in the ArrayList? Thanks

like image 686
irobotxx Avatar asked Oct 18 '12 17:10

irobotxx


People also ask

How do you retrieve all keys present in a HashMap in Java?

To retrieve the set of keys from HashMap, use the keyset() method. However, for set of values, use the values() method.

Can the value of a HashMap be an ArrayList?

A HashMap contains key-value pairs, there are three ways to convert a HashMap to an ArrayList: Converting the HashMap keys into an ArrayList. Converting the HashMap values into an ArrayList.

How do you get all keys of a map as a list in Java?

Use keySet() to Get a Set of Keys From a HashMap in Java The simplest way to get the keys from a HashMap in Java is to invoke the keySet() method on your HashMap object. It returns a set containing all the keys from the HashMap .


2 Answers

Why do you want to re-invent the wheel, when you already have something to do your work. Map.keySet() method gives you a Set of all the keys in the Map.

Map<String, Integer> map = new HashMap<String, Integer>();  for (String key: map.keySet()) {     System.out.println("key : " + key);     System.out.println("value : " + map.get(key)); } 

Also, your 1st for-loop looks odd to me: -

   for(int k = 0; k < list.size(); k++){             map = (HashMap)list.get(k);    } 

You are iterating over your list, and assigning each element to the same reference - map, which will overwrite all the previous values.. All you will be having is the last map in your list.

EDIT: -

You can also use entrySet if you want both key and value for your map. That would be better bet for you: -

    Map<String, Integer> map = new HashMap<String, Integer>();      for(Entry<String, Integer> entry: map.entrySet()) {         System.out.println(entry.getKey());         System.out.println(entry.getValue());     } 

P.S.: -
Your code looks jumbled to me. I would suggest, keep that code aside, and think about your design one more time. For now, as the code stands, it is very difficult to understand what its trying to do.

like image 110
Rohit Jain Avatar answered Sep 17 '22 17:09

Rohit Jain


List constructor accepts any data structure that implements Collection interface to be used to build a list.

To get all the keys from a hash map to a list:

Map<String, Integer> map = new HashMap<String, Integer>(); List<String> keys = new ArrayList<>(map.keySet()); 

To get all the values from a hash map to a list:

Map<String, Integer> map = new HashMap<String, Integer>(); List<Integer> values = new ArrayList<>(map.values()); 
like image 26
Sivabalan Avatar answered Sep 19 '22 17:09

Sivabalan