Java Collection Join joinSorted(Collection s, String delimiter)

Here you can find the source of joinSorted(Collection s, String delimiter)

Description

join Sorted

License

Open Source License

Declaration

@SuppressWarnings({ "unchecked", "rawtypes" })

    public static String joinSorted(Collection<? extends Comparable> s, String delimiter) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .jav  a  2s  .c  o m*/
 * Copyright (c) Erasmus MC
 *
 * This file is part of TheMatrix.
 *
 * TheMatrix is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.Collection;
import java.util.Collections;

import java.util.Iterator;
import java.util.List;

public class Main {
    @SuppressWarnings({ "unchecked", "rawtypes" })

    public static String joinSorted(Collection<? extends Comparable> s, String delimiter) {

        List list = new ArrayList(s);

        Collections.sort(list);

        return join(list, delimiter);

    }

    public static String join(Collection<?> s, String delimiter) {

        StringBuffer buffer = new StringBuffer();

        Iterator<?> iter = s.iterator();

        if (iter.hasNext()) {

            buffer.append(iter.next().toString());

        }

        while (iter.hasNext()) {

            buffer.append(delimiter);

            buffer.append(iter.next().toString());

        }

        return buffer.toString();

    }

    public static String join(Object[] objects, String delimiter) {

        StringBuffer buffer = new StringBuffer();

        if (objects.length != 0)
            buffer.append(objects[0].toString());

        for (int i = 1; i < objects.length; i++) {

            buffer.append(delimiter);

            buffer.append(objects[i].toString());

        }

        return buffer.toString();

    }
}

Related

  1. joinNullSafe(Collection collection, String separator)
  2. joinObjects(Collection objects)
  3. joinQuoted(Collection col)
  4. joinSequence(Collection sequence)
  5. joinSort(Collection c, String separator)
  6. joinString(Collection values, String delimiter)
  7. joinStringCollection(Collection collection, String separator)
  8. joinStrings(Collection strings)
  9. joinStrings(Collection names)

  10. HOME | Copyright © www.java2s.com 2016