Skip to content

Instantly share code, notes, and snippets.

@mmarcon
Created September 22, 2013 14:31
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mmarcon/6660453 to your computer and use it in GitHub Desktop.
Maps are not Parcelable and this is an issue in Android when they need to be passed to activities and services via Intents. The corresponding Map-like object in Android is the Bundle. Bundle is a more generic container, it doesn't enforce types via generics and isn't supported natively by JSON deserializers such as Gson. This utility class expos…
package es.cloudey.pagespeed.util;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.os.Parcelable;
public class CollectionUtils {
public static Bundle toBundle(Map<String, ? extends Parcelable> input) {
Bundle output = new Bundle();
for(String key : input.keySet()) {
output.putParcelable(key, input.get(key));
}
return output;
}
public static <T extends Parcelable> Map<String, T> fromBundle(Bundle input, Class<T> c) {
Map<String, T> output = new HashMap<String, T>();
for(String key : input.keySet()) {
output.put(key, c.cast(input.getParcelable(key)));
}
return output;
}
}
@hwangjr
Copy link

hwangjr commented Jan 20, 2016

You should try to use entry set to improve efficient.

@mochadwi
Copy link

You should try to use entry set to improve efficient.

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment