Java – Are android bundles functionally equivalent to maps?

Are android bundles functionally equivalent to maps?… here is a solution to the problem.

Are android bundles functionally equivalent to maps?

I’m looking for a way to convert a bundle to a HashMap. I’m surprised that this method is not available in docs .

Is a bundle equivalent to a Map with string keys and object values? Is it theoretically possible to convert to a map? Is there any way to do this?

Edit: Is the relationship between key and value clear in the bundle?

Solution

Is not the Bundle equivalent of a Map, where you have String keys and Object values?

First, a does not necessarily haveString a Mapkey.

Second, at the same time Mapcan hold arbitrary objects, which it cannot do so in a type-safe manner. IOW, if you want Mapto save strings, integers, etc., you need to continue converting get() the results. Phone, because your Mapmust be Map<String, Object> .

Third, tools Parcelable ,Bundle which are very important for Android IPC. Mapis an interface and does not extend Parcelable .

Can it be theoretically converted to a mapping?

Well, of course, although I don’t see any benefit.

What is the way to do this?

Iterate keySet()through and callget() on Bundle for each key, put the result into Map<String, Object> the .

Related Problems and Solutions