Java Collection Join join(String join_string, Collection c)

Here you can find the source of join(String join_string, Collection c)

Description

join

License

Open Source License

Declaration

public static final String join(String join_string, Collection c) 

Method Source Code


//package com.java2s;
/*// w w  w .  java  2 s  .c o  m
*   EuroCarbDB, a framework for carbohydrate bioinformatics
*
*   Copyright (c) 2006-2009, Eurocarb project, or third-party contributors as
*   indicated by the @author tags or express copyright attribution
*   statements applied by the authors.  
*
*   This copyrighted material is made available to anyone wishing to use, modify,
*   copy, or redistribute it subject to the terms and conditions of the GNU
*   Lesser General Public License, as published by the Free Software Foundation.
*   A copy of this license accompanies this distribution in the file LICENSE.txt.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
*   for more details.
*
*   Last commit: $Rev: 1593 $ by $Author: hirenj $ on $Date:: 2009-08-14 #$  
*/

import java.util.Map;

import java.util.Collection;

public class Main {
    /****************************************************
            *   
            *   Joins a list of strings (objects) by the given join string,
            *   as per the Perl function 'join'. Why this method isn't already
            *   in the language is a mystery. This method has the unusual form
            *   <tt>(String, Object, Object... )</tt> to eliminate compiler ambiguity
            *   when encountering code that uses <tt>join( String, Object[] )</tt>.
            *
            *   @param join_string 
            *   The string to be used for joining. 
            *   @param item
            *   The first object in the list of items to be joined.
            *   @param other_items
            *   A list of additional objects/strings to join.
            *   @return
            *   The joined string.
            */
    public static final String join(String join_string, Object item, Object... other_items) {
        if (other_items.length == 0)
            return item.toString();

        if (other_items.length == 1)
            return item.toString() + join_string + other_items[0];

        StringBuilder sb = new StringBuilder();
        sb.append(item);

        for (int i = 0; i < other_items.length; i++) {
            sb.append(join_string);
            sb.append(other_items[i]);
        }

        return sb.toString();
    }

    /****************************************************
            *   
            *   @see #join(String, Object, Object[])
            */
    public static final String join(String join_string, Object[] a) {
        //return join( join_string, c.toArray() );
        if (a == null)
            return null;
        if (a.length == 0)
            return "";

        StringBuilder sb = new StringBuilder();
        sb.append(a[0]);

        for (int i = 1; i < a.length; i++) {
            sb.append(join_string);
            sb.append(a[i]);
        }

        return sb.toString();
    }

    /****************************************************
            *   
            *   @see #join(String, Object[])
            */
    public static final String join(String join_string, Collection c) {
        return join(join_string, c.toArray());
    }

    /****************************************************
            * 
            *   Joins the list of strings given by the objects in the passed Map
            *   by the given join strings, as per the Perl function 'join'. 
            *   
            *   For example, the Map -
            *   <pre>
            *   Map m = {           // java should have a declarative syntax for Maps...
            *       name => "Matt"
            *       age  => 32
            *   }
            *   </pre>
            *   
            *   when called as <code> join( m, ": ", ", " ) </code> would return 
            *   <code> "name: Matt, age: 32" </code> as a result. Note that the order
            *   of key-value pairs in the final string is arbitrary -- use the 4-argument
            *   form of this method if you want the keys to appear in a specific order
            *   or if you want only certain keys/values printed.
            *   
            *   @param map
            *   The map.
            *   @param join_string1 
            *   The string to use to join key-value pairs from the Map.
            *   @param join_string2
            *   The string to use between joined key-value pairs from the Map.
            *   @return
            *   The joined string.
            *   @see #join(String, Object[])
            */
    public static final String join(Map map, String join_string1, String join_string2) {
        return join(map, join_string1, join_string2, map.keySet().toArray());
    }

    /****************************************************
            *   
            *   Same as the other join method for a Map, except the keys to be
            *   joined are explicitly given. No, this method doesn't check that 
            *   the given keys actually exist in the Map, that's your problem.
            *   
            *   @param map
            *   The map.
            *   @param join_string1 
            *   The string to use to join key-value pairs from the Map.
            *   @param join_string2
            *   The string to use between joined key-value pairs from the Map.
            *   @param keys
            *   An array which specifies which keys to be joined and in what order. 
            *   @return
            *   The joined string.
            *   @see #join(Map, String, String)
            */
    public static final String join(Map map, String join_string1, String join_string2, Object[] keys) {
        if (map == null || map.isEmpty())
            return "";

        StringBuilder sb = new StringBuilder(keys[0] + join_string1 + map.get(keys[0]));

        for (int i = 1; i < map.size(); i++) {
            sb.append(join_string2 + keys[i] + join_string1 + map.get(keys[i]));
        }

        return sb.toString();
    }
}

Related

  1. join(String glue, Collection c)
  2. join(String glue, Collection pieces)
  3. join(String glue, Collection c)
  4. join(String glue, Collection strings)
  5. join(String glue, Collection items)
  6. join(String joiner, Collection strings)
  7. join(String sep, Collection objects)
  8. join(String sep, Collection col)
  9. join(String sep, Collection col)