On the Adapter class How to get key and value from HashMap

use hashmap in recyclerview adapter
how to get all values from hashmap in java
how to display hashmap values in listview in android

On my RecyclerViewAdapter class i'm passing HashMap<String,ArrayList<Custom>> as data, now i want to get key & value for every position but i don't know how i should do this! thanks in advance

public class Main_Recycler_Single_Row extends RecyclerView.Adapter<Main_Recycler_Single_Row.ViewHolder> {



private Context mContext;
private HashMap<String,ArrayList<Products>> dataSet;

public Main_Recycler_Single_Row(Context mContext, HashMap<String,ArrayList<Products>> mDataSet) {
    this.mContext = mContext;
    this.dataSet = mDataSet;

} @Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

    Map.Entry<String,ArrayList<Products>> entry=(Map.Entry<String,ArrayList<Products>>) dataSet.entrySet();//this line is my problem!


    holder.header.setText(entry.getKey());

    holder.mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
    holder.mRecyclerView.setHasFixedSize(true);
    holder.mRecyclerView.setAdapter(new Main_Recycler_Sinle_Item(mContext,entry.getValue()));

}

If you are trying to get Value from HashMap using position, you also need to maintain Array of keys

keys = (String[])mDataSet.keySet().toArray();

Your ViewHolder class will look like:

public Main_Recycler_Single_Row(Context mContext, HashMap<String,ArrayList<Products>> mDataSet) {
    this.mContext = mContext;
    this.dataSet = mDataSet;
    keys = (String[])mDataSet.keySet().toArray();

} @Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {

    holder.header.setText(keys[position]);

    holder.mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
    holder.mRecyclerView.setHasFixedSize(true);
    holder.mRecyclerView.setAdapter(new Main_Recycler_Sinle_Item(mContext,dataSet.get(keys[position])));

}

What adapter shall I use to use HashMap in a ListView , I want to use HashMap for a list of items of Adapter for a ListView I was going to use IMHO, is generate a new list for they key values of the Hashmap that you are Map; public class MyAdapter extends BaseAdapter { private final ArrayList​  By making the Employee class implement the Object class's equals and hashcode methods, both the employee1 and employee2 hashcode and equals methods will return the same thing. The HashMap will use the hashcode and equals method to identify the bucket where the object is present and equals to check

You have to use entry.getvalue(); for getting values

String key = entry.getKey(); ArrayList<Products> value = entry.getValue();

Kotlin HashMap, The implementation of HashMap class does not make guarantees about the order of data of key, value and entries of collections. Constructor of Kotlin HashMap  value: which is the new value which has to be mapped with the provided key. Return Value: This method returns the previous value associated with the specified key. If there is no such key mapped, then it returns null , if the implementation supports null value.

Hope this works for you :

holder.header.setText(dataSet.getKey().toArray()[position]);// for key
holder.productDesc.setText(dataSet.get(position)
.get(productPosition).getDesc())  //sample for getting product description

Kotlin Program to Convert Map (HashMap) to List, File Operation I/O Class & Objects HashMap fun main(args: Array<String>) { val map = HashMap<Int, String>() Since map contains a key, value pair, we need two lists to store each of them, namely keyList for keys and valueList for values. We used map's keySet() method to get all the keys and created an ArrayList  Example of getting keys and values from HashMap in Java using the entrySet() method of HashMap. In this tutorial I will explain you how you can iterate through HashMap using the entrySet() method of HashMap class. You will be able to iterate the keys and get the values and print on the console.

To get value:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


    // key
    String key = (new ArrayList<>(tab.keySet())).get(position);
    // value
    Object value = Objects.requireNonNull(tab.get(key));

    TextView textView = holder.textRecyclerView;
    textView.setText(value);
}

HashMap, javax.xml.bind.annotation.adapters · javax.xml.bind.attachment (The HashMap class is roughly equivalent to Hashtable, except that it is increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). Returns a shallow copy of this HashMap instance: the keys and values  The Value mapped to Key 4 is:DD The Value mapped to Key 5 is:null Note: In the above program the key 5 is not mapped to any value so the get() method returned null, However you must not use this method for checking existence of a Key in HashMap because a return value of null does not necessarily indicate that the map contains no mapping for the key; it’s also possible that the map explicitly maps the key to null.

HashMap in Java with Example, HashMap is a Map based collection class that is used for storing Key & value the keys and values in the same order in which they have been inserted into the  Basic HashMap Operations. How to iterate through HashMap? How to copy Map content to another HashMap? How to search a key in HashMap? How to search a value in HashMap? How to get all keys from HashMap? How to get entry set from HashMap? How to delete all elements from HashMap? How to eliminate duplicate user defined objects as a key from HashMap?

How to convert HashMap to ArrayList in Java? • Crunchify, Here is a simple example on how to convert HashMap to ArrayList in Java. Java Example: public class CrunchifyHashmapToArrayList { create hashmap with keys and values (CompanyName, #Employees) getKey() + " = " + pairs. This enables you to easily modify the Adapter's data in MainActivity or wherever else you have the reference to an Adapter of this object. You would then need to call the adapter's notifyDataSetChanged() method to let it know that the list has changed so that it updates the ListView to reflect these changes.

Map Interface in Java: Implement Methods of Java Map Interface , SQLite DatabaseProgramming & FrameworksAndroid Adapter A Map in Java is an object that maps keys to values and is designed for the faster lookups. Now that you have an idea of what Map interface in Java is, let's go ahead And popular implementation classes of Map in Java are HashMap,  HashMap is a part of Java’s collection since Java 1.2. It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs. To access a value one must know its key. HashMap is known as HashMap because it uses a technique called Hashing. Hashing is a technique of converting a large String to small String

Comments
  • what's the problem ? any error ?
  • warning:unchecked cast , on the Map.Entry line