简体   繁体   中英

How to get Hashmap Data

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?

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);
   }
}

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();
}

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.

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.

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));
    }
}
  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
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM