Java Collection Join join(Collection col, String delim)

Here you can find the source of join(Collection col, String delim)

Description

join

License

Open Source License

Declaration

public static String join(Collection<?> col, String delim) 

Method Source Code

//package com.java2s;
/**//from  www .  j  a v  a 2  s .  c  o m
 * Tencent is pleased to support the open source community by making MSEC available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the GNU General Public License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. You may 
 * obtain a copy of the License at
 *
 *     https://opensource.org/licenses/GPL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the 
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

import java.util.*;

public class Main {
    public static String join(Collection<?> col, String delim) {
        StringBuilder sb = new StringBuilder();
        Iterator<?> iter = col.iterator();
        if (iter.hasNext())
            sb.append(iter.next().toString());
        while (iter.hasNext()) {
            sb.append(delim);
            sb.append(iter.next().toString());
        }
        return sb.toString();
    }

    public static String join(Map<?, ?> map, String delim) {
        StringBuilder sb = new StringBuilder();
        Iterator<?> iter = map.entrySet().iterator();
        if (iter.hasNext()) {
            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) iter.next();
            sb.append(entry.getKey() + ":" + entry.getValue());
        }
        while (iter.hasNext()) {
            sb.append(delim);
            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) iter.next();
            sb.append(entry.getKey() + ":" + entry.getValue());
        }
        return sb.toString();
    }
}

Related

  1. join(Collection strs, String separator)
  2. join(Collection c, String delim)
  3. join(Collection c, String delimiter)
  4. join(Collection c, String insert)
  5. join(Collection c, String separator)
  6. join(Collection col, String separator)
  7. join(Collection coll, String delim)
  8. join(Collection collection, String delimiter)
  9. join(Collection collection, String delimiter)