0

I had seen many examples regarding Hashmap Data but I am not getting the data as required.

Here is my code:

HashMap<String,ArrayList<String>> citylist = new HashMap<String, ArrayList<String>>();

    ArrayList<String> Gujarat = new ArrayList<String>();
    Gujarat.add("Surat");
    Gujarat.add("Baroda");
    Gujarat.add("Ahmedabad");

    ArrayList<String> Rajasthan = new ArrayList<String>();
    Rajasthan.add("Udaipur");
    Rajasthan.add("Jaipur");

    ArrayList<String> UP= new ArrayList<String>();
    UP.add("Lucknow");
    UP.add("Agra");

    citylist.put("Gujarat", Gujarat);
    citylist.put("UP", UP);
    citylist.put("Rajasthan", Rajasthan);

It is in recyclerview how to get this type of data in BindViewHolder? Toast is coming like:

   {Rajasthan=[Udaipur, Jaipur], UP=[Lucknow, Agra], Gujarat=[Surat, Baroda, Ahmedabad]}

I had used this method to get but error is coming:

    public void onBindViewHolder(MyViewHolder holder, int position) {
    ArrayList<String> lst = citylist.get("" + position);
    for (Integer i = 0; i < lst.size(); i++) {
        holder.tv.setText(citylist.toString());
        Log.e("Hashmap....", ""+holder.tv );
    }

the output should be like Gujarat is state and surat baroda and ahmedabad are cities?

Abhi
  • 385
  • 1
  • 4
  • 13

6 Answers6

1

First create one ArrayList with all state :

ArrayList<String> stateList = new ArrayList<String>();
stateList.add("Gujarat");
stateList.add("UP");
stateList.add("Rajasthan");

Second create one HashMap with each state name as Key and each state city as Value:

HashMap<String,ArrayList<String>> stateCityMap = new HashMap<String,ArrayList<String>>()

ArrayList<String> gujaratCityList = new ArrayList<String>();
gujaratCityList.add("Ahmedabad");
gujaratCityList.add("Surat");
gujaratCityList.add("Baroda");
.......................

ArrayList<String> upCityList = new ArrayList<String>();
upCityList.add("Lucknow");
upCityList.add("Agra");
..........................

ArrayList<String> rajasthanCityList = new ArrayList<String>();
rajasthanCityList.add("Udaipur");
rajasthanCityList.add("Jaipur");
...........................

stateCityMap.put("Gujarat",gujaratCityList);
stateCityMap.put("UP",upCityList);
stateCityMap.put("Rajasthan",rajasthanCityList);

Now get all city name based on state in Adapter :

public void onBindViewHolder(MyViewHolder holder, int position) {
   Log.e("State : ",stateList.get(position));
   ArrayList<String> cityList=   (ArrayList<String>)stateCityMap.get(stateList.get(position));
   for(String cityName : cityList){
      Log.e("City : ",cityName);
   }
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

you can get like below.

Iterator iterator = map.keySet().iterator();
while(iterator.hasNext()) {
    String key=(String)iterator.next();
    String value=(String)map.get(key);
    Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
}
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
0

HashMaps do not preserve ordering:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Take a look at LinkedHashMap, which guarantees a predictable iteration order.

Prashant Sharma
  • 1,357
  • 1
  • 21
  • 31
0

You might wanna try something like this.

for (Map.Entry<String, ArrayList<String>> entry : citylist.entrySet()) 
{
    String key = entry.getKey();        // Your State
    String value = entry.getValue();    // Your List of Cities.

    // Split data and insert in Views
}

However I recommend (for easy to use case) keep a List of all the states and get Value from HashMap using keys from this List of states.

Abbas
  • 3,529
  • 5
  • 36
  • 64
  • brada i got your answer but it showing me last one ie Gujarat Gujarat Gujarat.How can I get all Possible value? – Abhi Sep 01 '16 at 05:36
  • Are you getting last key over and over? Are you using key inside or outside the loop? – Abbas Sep 01 '16 at 05:43
  • The code is fine there seems to be some trouble implementing it from your end, check again and if still don't see any difference then update your code in the question. – Abbas Sep 01 '16 at 06:01
0

Please check it :

    ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String,String>>();
    HashMap<String, String> h1 = new HashMap<String, String>();
    h1.put("h1_key_1", "h1_value_1");
    h1.put("h1_key_2", "h1_value_2");
    arrayList.add(h1);

    for (HashMap<String, String> hashMap : arrayList) {
    System.out.println(hashMap.keySet());
    for (String key : hashMap.keySet()) {
    System.out.println(hashMap.get(key));
    }
}
kgandroid
  • 5,507
  • 5
  • 39
  • 69
Chirag Arora
  • 816
  • 8
  • 20
0
  Try This:

    public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
    it.remove(); // avoids a ConcurrentModificationException
}
}
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49