Java Map Join Join(Map map, String entryGlue, String elementGlue)

Here you can find the source of Join(Map map, String entryGlue, String elementGlue)

Description

Joins a map together (using StringBuilder's {

License

Open Source License

Parameter

Parameter Description
map The map to concatenate
entryGlue The glue to use between the key and value of each pair in the map
elementGlue The glue to use between each key-value element pairs in the map

Return

The concatenated string

Declaration

public static String Join(Map map, String entryGlue, String elementGlue) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    /**//from  ww  w.  ja  va2 s. c o m
     * Joins a map together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object)
     *
     * @param map The map to concatenate
     * @param entryGlue The glue to use between the key and value of each pair
     * in the map
     * @param elementGlue The glue to use between each key-value element pairs
     * in the map
     * @return The concatenated string
     */
    public static String Join(Map map, String entryGlue, String elementGlue) {
        return Join(map, entryGlue, elementGlue, null, null, null);
    }

    /**
     * Joins a map together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object)
     *
     * @param map The map to concatenate
     * @param entryGlue The glue to use between the key and value of each pair
     * in the map
     * @param elementGlue The glue to use between each key-value element pairs
     * in the map
     * @param lastElementGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the map, then this glue
     * is used instead. If it is null, then lastElementGlue is used instead.
     * @param empty If the map is completely empty, this string is simply
     * returned. If null, an empty string is used.
     * @return The concatenated string
     */
    public static String Join(Map map, String entryGlue, String elementGlue, String lastElementGlue) {
        return Join(map, entryGlue, elementGlue, lastElementGlue, null, null);
    }

    /**
     * Joins a map together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object)
     *
     * @param map The map to concatenate
     * @param entryGlue The glue to use between the key and value of each pair
     * in the map
     * @param elementGlue The glue to use between each key-value element pairs
     * in the map
     * @param lastElementGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the map, then this glue
     * is used instead. If it is null, then lastElementGlue is used instead.
     * @return The concatenated string
     */
    public static String Join(Map map, String entryGlue, String elementGlue, String lastElementGlue,
            String elementGlueForTwoItems) {
        return Join(map, entryGlue, elementGlue, lastElementGlue, elementGlueForTwoItems, null);
    }

    /**
     * Joins a map together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object)
     *
     * @param map The map to concatenate
     * @param entryGlue The glue to use between the key and value of each pair
     * in the map
     * @param elementGlue The glue to use between each key-value element pairs
     * in the map
     * @param lastElementGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the map, then this glue
     * is used instead. If it is null, then lastElementGlue is used instead.
     * @param empty If the map is completely empty, this string is simply
     * returned. If null, an empty string is used.
     * @return The concatenated string
     */
    public static String Join(Map map, String entryGlue, String elementGlue, String lastElementGlue,
            String elementGlueForTwoItems, String empty) {
        //Just create a list of glued together entries, then send it to the other Join method
        List<String> list = new ArrayList<String>();
        for (Object key : map.keySet()) {
            StringBuilder b = new StringBuilder();
            b.append(key).append(entryGlue).append(map.get(key));
            list.add(b.toString());
        }
        return Join(list, elementGlue, lastElementGlue, elementGlueForTwoItems, empty);
    }

    /**
     * Joins a set together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue.
     * @param list The set to concatenate
     * @param glue The glue to use
     * @return The concatenated string
     */
    public static String Join(Set set, String glue) {
        return Join(set, glue, null, null, null);
    }

    /**
     * Joins a set together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for sets that are being read by a human, to have a proper
     * conjunction at the end.
     * @param list The set to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @return The concatenated string
     */
    public static String Join(Set set, String glue, String lastGlue) {
        return Join(set, glue, lastGlue, null, null);
    }

    /**
     * Joins a set together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for sets that are being read by a human, to have a proper
     * conjunction at the end.
     * @param list The set to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the set, then this glue
     * is used instead. If it is null, then lastGlue is used instead.
     * @return The concatenated string
     */
    public static String Join(Set set, String glue, String lastGlue, String glueForTwoItems) {
        return Join(set, glue, lastGlue, glueForTwoItems, null);
    }

    /**
     * Joins a set together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for sets that are being read by a human, to have a proper
     * conjunction at the end.
     * @param list The set to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the set, then this glue
     * is used instead. If it is null, then lastGlue is used instead.
     * @param empty If the set is completely empty, this string is simply
     * returned. If null, an empty string is used.
     * @return The concatenated string
     */
    public static String Join(Set set, String glue, String lastGlue, String glueForTwoItems, String empty) {
        List list = new ArrayList(set);
        if (lastGlue == null) {
            lastGlue = glue;
        }
        if (glueForTwoItems == null) {
            glueForTwoItems = lastGlue;
        }
        if (list.isEmpty()) {
            return empty == null ? "" : empty;
        } else if (list.size() == 2) {
            StringBuilder b = new StringBuilder();
            return b.append(list.get(0)).append(glueForTwoItems).append(list.get(1)).toString();
        } else {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                Object o = list.get(i);
                if (i != 0) {
                    if (i == list.size() - 1) {
                        b.append(lastGlue);
                    } else {
                        b.append(glue);
                    }
                }
                b.append(o);
            }
            return b.toString();
        }
    }

    /**
     * Joins an array together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue.
     * @param list The array to concatenate
     * @param glue The glue to use
     * @return The concatenated string
     */
    public static String Join(Object[] list, String glue) {
        return Join(list, glue, null, null, null);
    }

    /**
     * Joins an array together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for lists that are being read by a human, to have a
     * proper conjunction at the end.
     * @param list The array to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @return The concatenated string
     */
    public static String Join(Object[] list, String glue, String lastGlue) {
        return Join(list, glue, lastGlue, null, null);
    }

    /**
     * Joins an array together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for lists that are being read by a human, to have a
     * proper conjunction at the end.
     * @param list The array to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the array, then this glue
     * is used instead. If it is null, then lastGlue is used instead.
     * @return The concatenated string
     */
    public static String Join(Object[] list, String glue, String lastGlue, String glueForTwoItems) {
        return Join(list, glue, lastGlue, glueForTwoItems, null);
    }

    /**
     * Joins an array together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for lists that are being read by a human, to have a
     * proper conjunction at the end.
     * @param list The array to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the array, then this glue
     * is used instead. If it is null, then lastGlue is used instead.
     * @param empty If the array is completely empty, this string is simply
     * returned. If null, an empty string is used.
     * @return The concatenated string
     */
    public static String Join(Object[] list, String glue, String lastGlue, String glueForTwoItems, String empty) {
        if (lastGlue == null) {
            lastGlue = glue;
        }
        if (glueForTwoItems == null) {
            glueForTwoItems = lastGlue;
        }
        if (list.length == 0) {
            return empty == null ? "" : empty;
        } else if (list.length == 2) {
            StringBuilder b = new StringBuilder();
            return b.append(list[0]).append(glueForTwoItems).append(list[1]).toString();
        } else {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < list.length; i++) {
                Object o = list[i];
                if (i != 0) {
                    if (i == list.length - 1) {
                        b.append(lastGlue);
                    } else {
                        b.append(glue);
                    }
                }
                b.append(o);
            }
            return b.toString();
        }
    }

    /**
     * Joins a list together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue.
     * @param list The list to concatenate
     * @param glue The glue to use
     * @return The concatenated string
     */
    public static String Join(List list, String glue) {
        return Join(list, glue, null, null, null);
    }

    /**
     * Joins a list together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for lists that are being read by a human, to have a
     * proper conjunction at the end.
     * @param list The list to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @return The concatenated string
     */
    public static String Join(List list, String glue, String lastGlue) {
        return Join(list, glue, lastGlue, null, null);
    }

    /**
     * Joins a list together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for lists that are being read by a human, to have a
     * proper conjunction at the end.
     * @param list The list to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the list, then this glue
     * is used instead. If it is null, then lastGlue is used instead.
     * @return The concatenated string
     */
    public static String Join(List list, String glue, String lastGlue, String glueForTwoItems) {
        return Join(list, glue, lastGlue, glueForTwoItems, null);
    }

    /**
     * Joins a list together (using StringBuilder's {
     *
     * @see StringBuilder#append(Object)} method to "toString" the Object) using
     * the specified string for glue. If lastGlue is null, it is the same as
     * glue, but otherwise it is used to glue just the last two items together,
     * which is useful for lists that are being read by a human, to have a
     * proper conjunction at the end.
     * @param list The list to concatenate
     * @param glue The glue to use
     * @param lastGlue The glue for the last two elements
     * @param glueForTwoItems If only two items are in the list, then this glue
     * is used instead. If it is null, then lastGlue is used instead.
     * @param empty If the list is completely empty, this string is simply
     * returned. If null, an empty string is used.
     * @return The concatenated string
     */
    public static String Join(List list, String glue, String lastGlue, String glueForTwoItems, String empty) {
        if (lastGlue == null) {
            lastGlue = glue;
        }
        if (glueForTwoItems == null) {
            glueForTwoItems = lastGlue;
        }
        if (list.isEmpty()) {
            return empty == null ? "" : empty;
        } else if (list.size() == 2) {
            StringBuilder b = new StringBuilder();
            return b.append(list.get(0)).append(glueForTwoItems).append(list.get(1)).toString();
        } else {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                Object o = list.get(i);
                if (i != 0) {
                    if (i == list.size() - 1) {
                        b.append(lastGlue);
                    } else {
                        b.append(glue);
                    }
                }
                b.append(o);
            }
            return b.toString();
        }
    }
}

Related

  1. join(final long timeout, final Map throwableMap, final Thread... threads)
  2. join(final Map map1, final Map map2, final Map... maps)
  3. join(final Map add, final Map... imports)
  4. join(List> list)
  5. join(Map dst, Map src)
  6. join(Map map, String separator)
  7. joinAll(Map> map, String separator)
  8. joinArgs(Map map, String sep, String split)