Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store a data structure such as a Hashmap internally in Android?

In my Android application I'm trying to store a Map structure such as:Map<String, Map<String, String>>using internal storage. I've looked into using SharedPreferences, but as you know, this only works when storing primitive data types. I tried to use FileOutputStream, but it only lets me write in bytes...Would I need to somehow serialize the Hashmap and then write to file?

I've tried reading through http://developer.android.com/guide/topics/data/data-storage.html#filesInternal but I can't seem to find my solution.

Here's an example of what I'm trying to do:

private void storeEventParametersInternal(Context context, String eventId, Map<String, String> eventDetails){

Map<String,Map<String,String>> eventStorage = new HashMap<String,Map<String,String>>();
Map<String, String> eventData = new HashMap<String, String>();
String REQUEST_ID_KEY = randomString(16);
.   //eventData.put...
.   //eventData.put...
eventStorage.put(REQUEST_ID_KEY, eventData);
FileOutputStream fos = context.openFileOutput(EVENT_FILENAME, Context.MODE_PRIVATE);
fos.write(eventStorage) //This is wrong but I need to write to file for later access..

}

What is the best approach for storing this type of a data structure internally in an Android App? Sorry if this seems like a dumb question, I am very new to Android. Thanks in advance.

like image 402
Atticus Avatar asked Jul 18 '13 15:07

Atticus


People also ask

How HashMap is implemented internally?

Internally HashMap uses a hashCode of the key Object and this hashCode is further used by the hash function to find the index of the bucket where the new entry can be added. HashMap uses multiple buckets and each bucket points to a Singly Linked List where the entries (nodes) are stored.

What data structure is used in HashMap?

It uses an array and LinkedList data structure internally for storing Key and Value. There are four fields in HashMap.

What is HashMap in Android Studio?

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.)


1 Answers

HashMap is serializable, so you could just use a FileInputStream and FileOutputStream in conjunction with ObjectInputStream and ObjectOutputStream.

To write your HashMap to a file:

FileOutputStream fileOutputStream = new FileOutputStream("myMap.whateverExtension");
ObjectOutputStream objectOutputStream= new ObjectOutputStream(fileOutputStream);

objectOutputStream.writeObject(myHashMap);
objectOutputStream.close();

To read the HashMap from a file:

FileInputStream fileInputStream  = new FileInputStream("myMap.whateverExtension");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

Map myNewlyReadInMap = (HashMap) objectInputStream.readObject();
objectInputStream.close();
like image 136
Steve P. Avatar answered Nov 01 '22 23:11

Steve P.