如何将 HashMap<Integer,ArrayList<String>> 存储到 SQLite?



我必须在使用共享偏好重新启动应用程序时将hashmap值存储在sqlite上并重复使用。

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

问题编辑之前的原始答案:
Hashmap已经被序列化,因此您可以将哈希图直接存储在sqlite数据库上作为blob。虽然您将无法阅读它的内容,直到您将其删除为止。

编辑:
您应该将长期运行的操作放在异步任务中。如果您希望对变量的引用可以在配置更改中生存,则应使用片段并使用setRetainInstance(true),尽管无论您保持对异步的参考,它都与UI线程无关,并且会继续运行。

>

旁注:
使用ArrayList时,始终使用手动计数循环,因为它具有更好的性能。

在此比较中可以看出,使用计数器设置为阵列的尺寸明显快于每个环的循环:

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

最新更新