3

I'm a newbie to Android, however I'm not entirely new to Java. From what I can tell, the ArrayMap should be used over the HashMap due to less memory used/performance increase. I'm not entirely sure when I should use one over the other beyond that, however while attempting to send an ArrayMap over a Bundle/Intent, I noticed that I was unable to do so, yet I could with a HashMap.

So, I thought, instead of just succumbing to using HashMap, especially when it's not necessary to, I'll create my own Serializable ArrayMap, however I'm unsure as to how to go about it.

All I know is that I implement three methods, readObject, writeObject, and readObjectNoData, all which have a Stream as an argument. I'm assuming I have to go through each item in the ArrayMap, then write it to the output stream and read from the input stream to recreate the ArrayMap. What I want to know is... how I should go about doing so.

I'm sorry if there is a duplicate question, but I can't really find any at all. So I'll start off with an example to fend off any "YAGNI" (You Aren't Gonna Need It). For one of my projects, I maintained a nested ArrayMap of objects, with said objects being in the thousands, with nested key-values to reduce collision greatly. I can't go too far into it, as it was for a recent school project. I wanted to be able to preserve the data structure over the life cycle of the application, including onDestroy of MainActivity, without having to parse everything all over again.

My solution ended up being to create a constant (static final) ArrayMap and access that, which worked, but it feels like I'm going against the philosophy of Android Development. Also I'm assuming that when MainActivity is destroyed the constant is also destroyed as well, yet with a saved bundle, it will be preserved, which is what I want.

I'm getting off topic... anyway, I want to create a Serializable ArrayMap, but I have a lot of questions regarding it. For one, what would be the best format to represent the objects nested inside of an ArrayMap? JSON would be my guess, although I've never actually done anything with it before. Then, how do I get the information from the objects nested inside? Do I enforce that only certain objects that implement an interface I declare may be added to the map? Then HashMap doesn't have this requirement. Do I use reflection to obtain each field and value, but then isn't that super slow?

I cannot use any third-party libraries, however I would love to do this from scratch regardless as it feels like a good way to learn more, does anyone have any tips/ideas on how to properly approach this? Thank you in advance!

Louis Jenkins
  • 391
  • 1
  • 2
  • 10
  • Just curious, after looking around a bit, would it be as simple as going through each Key-Value pair and calling writeObject on each Key-Value pair? If so, I feel stupid for asking this. – Louis Jenkins Oct 21 '15 at 03:11
  • Stuff getting out of hand now, I can't figure out which direction to go. ArrayMap is final but I managed to create delegate methods for everything it has, as a kind of wrapper class. Then I realized that I can't just use Serializable as the objects themselves aren't serializable, which throws a bunch of errors. Then I read I should use Parcelable instead (?), but now in order to use it generically I gotta go head-first into reflection, and in particular, make a ClassLoader, which I have no idea what it is... sounds fun though. – Louis Jenkins Oct 21 '15 at 04:35
  • Did you find a work around this ? – Juan Ricardo May 07 '20 at 22:46

0 Answers0