2

I have an Activity which first needs to determine the output based on input, with a help of HashMap. I am storing 25 pairs in it.

The problem is that HashMap initializes every time the activity starts. That got me thinking about the best way to store these pairs - should they be in an "external resource", or maybe some other solution - to make pairs persistent?

c0dehunter
  • 6,412
  • 16
  • 77
  • 139
  • I guess 25 pairs it's nothing! ) – Yury Jul 13 '12 at 20:59
  • Do you mean that it's not that consuming to bother with it? – c0dehunter Jul 13 '12 at 21:02
  • is it declared static? can you trick it into not being garbage collected somehow? Why is it that memory leaks are so hard to do in java, unless you're trying to avoid them? :S – Wug Jul 13 '12 at 21:05
  • If the pairs never change then you could just create a `SheredPreferences` file exclusively for those 25 pairs. You'd need to iterate through the `HashMap` and put each pair individually when you first save them to the `SharedPreferences` but getting it back could be simply done using the `getAll()` method. A bit unconventional perhaps but it should work. – Squonk Jul 13 '12 at 21:11
  • 1
    I think you should not bother about this. Just create a private HashMap object. 25 pairs consumes not so much memory. I do not think that it's better to use SharedPreferences. They are working with files and still you need to allocate memory to work with their values. – Yury Jul 14 '12 at 19:26
  • Yury, that's what I ended up doing. – c0dehunter Jul 14 '12 at 20:07

2 Answers2

1

Putting the data from your hashmap into some persistent storage will probably not help you to get around initializing the hashmap when creating your activity. If your application frequently uses this activity it might be more worthwhile storing the Hashmap as a field in the Application class and then accessing from any activity via the application context.

    SampleApplication extends Application{
        private HashMap<String,String> mMap = null;

        public void onCreate(){
            //initialize your hashmap here
            ...
        }

        public getHashMap{
            return this.mMap;
        }
     }

To get the map from any activity in your project just use the following code:

    HashMap<String,String> map = ((SampleApplication)getApplicationContext()).getHashMap();

If you still need to store the data persistently you can choose one of five methods listed here. HashMap does implement Serializable so I would probably use SharedPreferences if it were me.

EDIT Mert's answer is great if the string's you're using never change, just use string resources instead. But if you need to do a fair amount of manipulation the convenience of a collection might still make the approach in my answer worth it.

TJ Thind
  • 784
  • 5
  • 17
0

It depends how use your Strings..

You can store them on String XML if all fixed and perminent.

You can make a class and store in ApplicationContext if you use only application runs.

You can keep in file/sqlite db or user settings.

Mert
  • 6,432
  • 6
  • 32
  • 68
  • How could I acomplish HashMap equivalent using String XML? – c0dehunter Jul 13 '12 at 21:01
  • here is an example http://stackoverflow.com/questions/10598250/how-to-convert-string-xml-to-mapstring-string?lq=1 http://stackoverflow.com/questions/1537207/how-to-convert-xml-to-java-util-map-and-vice-versa – Mert Jul 13 '12 at 21:04
  • Rather than using a map at all if the string value's never change during run-time then use two sets of string array resources. [Documentation](http://developer.android.com/guide/topics/resources/string-resource.html) – TJ Thind Jul 13 '12 at 21:37