map To Key AND Value String - Android java.util

Android examples for java.util:Map

Description

map To Key AND Value String

Demo Code


//package com.java2s;
import android.text.TextUtils;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Main {

    public static String mapToKeyANDValueString(Map<String, String> map,
            String spilt, String pairSeparator) {
        if (isEmpty(map)) {
            return null;
        }//  w w  w .j  a  v a 2 s  .  com

        if (TextUtils.isEmpty(spilt)) {
            spilt = "&";
        }

        if (TextUtils.isEmpty(pairSeparator)) {
            pairSeparator = "=";
        }

        StringBuilder builder = new StringBuilder();
        Iterator<Entry<String, String>> ite = map.entrySet().iterator();
        while (ite.hasNext()) {
            Entry<String, String> entry = (Entry<String, String>) ite
                    .next();
            builder.append(entry.getKey()).append(pairSeparator)
                    .append(entry.getValue());
            if (ite.hasNext()) {
                builder.append(spilt);
            }
        }
        return builder.toString();
    }

    public static <K, V> boolean isEmpty(Map<K, V> sourceMap) {
        return (sourceMap == null || sourceMap.size() == 0);
    }
}

Related Tutorials