How to store HashMap <Integer, ArrayList <String> for SQLite?

I need to store the HashMap value on SQLite using Keyset and reuse it when the app is restarted using shared preference.

HashMap<Integer, ArrayList<String>> hashMap;
hashMap = new HashMap<>();
//Insert Value
hashMap.put(btn.getId(), listValue);
// Read a Value
Map.Entry<Integer, ArrayList<String>> entry = (Map.Entry<Integer,   ArrayList<String>>) iteratorMap.next();

for (Integer ihashId :hashMap.keySet()) {
    if( btnid == ihashId)
     {
       Set<Map.Entry<Integer, ArrayList<String>>> setMap = hashMap.entrySet();
       Iterator<Map.Entry<Integer,  ArrayList<String>>> iteratorMap =  setMap.iterator();
       while (iteratorMap.hasNext()) {
             Map.Entry<Integer, ArrayList<String>> entry = (Map.Entry<Integer, ArrayList<String>>) iteratorMap.next();
             ArrayList<String> values = entry.getValue();
             if (btnId == entry.getKey()) {
               getSetName.setText(values.get(0));
               getSetAddress.setText(values.get(1));
               getSetPin.setText(values.get(2));
               getSetValue.setText(values.get(3));
        }
    }

 }

      

+3


source to share


1 answer


Original answer before question Edited: The
HashMap is already serializable, so you can store the HashMap directly in your sqlite database as a BLOB. Though you won't be able to read its contents until you serialize it.

EDIT:
You have to put long running operations inside an async task. If you want the variable reference to survive configuration changes, you should use the snippet and use setRetainInstance(true)

, although whether or not you keep your reference to the AsyncTask it is independent of the UI thread and will continue to work.

NOTE:
Always use a manual computed loop when using ArrayList as it has better performance.



As you can see from this comparison , using the array size for a loop with a set counter is much faster than for each loop:

private static List<Integer> list = new ArrayList<>();
for(int j = list.size(); j > size ; j--)
{
    //do stuff
}

      

0


source







All Articles