Join keys elements of the HashMap with a glue String and quote each one of them with a quote string. - Java java.lang

Java examples for java.lang:String Quote

Description

Join keys elements of the HashMap with a glue String and quote each one of them with a quote string.

Demo Code


//package com.java2s;

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

public class Main {
    /**//from  w w  w.  java 2  s. c  o m
     * Join keys elements of the HashMap with a glue String and quote each
     * one of them with a quote string. The values are ignored.
     *
     * @param glue
     * the connecting string, for instance a comma
     * @param pieces
     * the parts to be join
     * @param quote
     * the quote to use
     * @return a String containing a String representation of all the array
     * elements in the same order, with the glue String between each
     * element.
     */
    public static String implodeAndQuote(final String glue,
            final Map<String, Object> pieces, final String quote) {
        StringBuilder sb = new StringBuilder();
        String sep = "";
        Iterator<Entry<String, Object>> it = pieces.entrySet().iterator();
        while (it.hasNext()) {
            sb.append(sep).append(quote).append((it.next()).getKey())
                    .append(quote);
            sep = glue;
        }
        return sb.toString();
    }

    /**
     * Join keys elements of the String array with a glue Stringand quote
     * each one of them with a quote string.
     *
     * @param glue
     * the connecting string, for instance a comma
     * @param pieces
     * the parts to be join
     * @param quote
     * the quote to use
     * @return a String containing a String representation of all the array
     * elements in the same order, with the glue String between each
     * element.
     */
    public static String implodeAndQuote(final String glue,
            final String[] pieces, final String quote) {
        StringBuilder sb = new StringBuilder();
        String sep = "";
        for (String piece : pieces) {
            sb.append(sep).append(quote).append(piece).append(quote);
            sep = glue;
        }
        return sb.toString();
    }
}

Related Tutorials