Write a HashMap to a Parcel, class of key and value can parcelable both - Android android.os

Android examples for android.os:Parcel

Description

Write a HashMap to a Parcel, class of key and value can parcelable both

Demo Code


//package com.java2s;

import java.util.Map;
import java.util.Map.Entry;
import android.os.Parcel;
import android.os.Parcelable;

public class Main {
    /**/* w  ww  . ja va 2  s .  c om*/
     * Write a HashMap to a Parcel, class of key and value can parcelable both
     *
     * @param map
     * @param out
     * @param flags
     */
    public static <K extends Parcelable, V extends Parcelable> void writeHashMap(
            Map<K, V> map, Parcel out, int flags) {
        if (map != null) {
            out.writeInt(map.size());

            for (Entry<K, V> entry : map.entrySet()) {
                out.writeParcelable(entry.getKey(), flags);
                out.writeParcelable(entry.getValue(), flags);
            }
        } else {
            out.writeInt(-1);
        }
    }
}

Related Tutorials