Java String Join join(HashMap params, String separator)

Here you can find the source of join(HashMap params, String separator)

Description

join

License

Apache License

Declaration

public static String join(HashMap<String, String> params, String separator) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/*  www  .j  a va 2 s  .co  m*/
     * Joins list of string items to a single string, where items are separated
     * with a defined separator.
     *
     * @param list      to join into string
     * @param separator to be used between elements
     * @return items joined into a single string
     */
    public static String join(List<?> list, String separator) {

        if (separator == null || separator.equalsIgnoreCase("")) {
            throw new IllegalArgumentException("Missing separator!");
        }

        String output = "";

        if (list != null && list.size() > 0) {
            for (int i = 1; i <= list.size(); i++) {
                output = output + list.get(i - 1);
                if (i < list.size()) {
                    output = output + separator;
                }
            }
        }

        return output;
    }

    /**
     * Joins list of string items to a single string, where items are separated
     * with a defined separator.
     *
     * @param args      to join into string
     * @param separator to be used between elements
     * @return items joined into a single string
     */
    public static String join(Object[] args, String separator) {

        if (separator == null || separator.equalsIgnoreCase("")) {
            throw new IllegalArgumentException("Missing separator!");
        }

        if (args != null && args.length > 0) {
            List<Object> array = Arrays.asList(args);
            return join(array, separator);
        }

        return "";
    }

    /**
     * Joins list of string items to a single string, where items are separated
     * with a defined separator.
     *
     * @param items     to join into string
     * @param separator to be used between elements
     * @return items joined into a single string
     */
    public static String join(Set<String> items, String separator) {

        if (separator == null || separator.equalsIgnoreCase("")) {
            throw new IllegalArgumentException("Missing separator!");
        }

        if (items != null && items.size() > 0) {
            Object[] array = items.toArray();
            return join(array, separator);
        }

        return "";
    }

    public static String join(HashMap<String, String> params, String separator) {

        if (separator == null || separator.equalsIgnoreCase("")) {
            throw new IllegalArgumentException("Missing separator!");
        }

        String output = "";
        if (params != null) {
            for (String name : params.keySet()) {
                if (output.length() > 0) {
                    output = output + separator;
                }

                output = output + name + "=" + params.get(name);
            }
        }

        return output;
    }
}

Related

  1. join(final Collection input, final String link)
  2. join(final String sep, final Iterable strs)
  3. join(final String separator, final Collection objs)
  4. join(final String separator, final String... strings)
  5. join(final String... arguments)
  6. join(Iterable arr, String delimeter)
  7. join(Iterable input)
  8. join(java.util.Collection strings, String delimiter)
  9. join(Map map, String separator)